2017-11-11 225 views
0

等待我有問題,巴貝爾38年5月8日,等待中的Node.js 我的代碼看起來像:巴貝爾38年8月5日無視Node.js的

/** 
* Imports 
*/ 
import {signature} from '../core/antpool'; 
import log from '../core/logging'; 
import config from '../config'; 
import {sendLog as sendEmailLog, EmailTemplate} from '../core/email'; 
import {rethinkdb, Decorators as DBDecorators} from '../core/db'; 
import request from 'request'; 

const tables = { 
    AntpoolAccount: 'CronAntpoolAccount' 
}; 

class Antpool { 

    @DBDecorators.table(tables.AntpoolAccount) 
    static async account(coin) { 
     var nonce = Date.now(); 
     request.post({ 
      url: config.antpool.baseUrl + '/api/account.htm', 
      json: true, 
      form: { 
       key: config.antpool.key, 
       nonce: nonce, 
       signature: signature(nonce), 
       coin: coin 
      } 
     }, function (err, httpResponse, body) { 
      if (err || httpResponse.statusCode != 201) { 
       log.error(err, '[CRON][Antpool][Account] Connection problem'); 
       sendEmailLog(EmailTemplate.LOG, { 
        message: '[CRON][Antpool][Account] Connection problem' 
       }); 
       return false; 
      } 

      var out = JSON.parse(body); 
      if (out.code != 0) { 
       log.error(err, '[CRON][Antpool][Account] Error response('+out.code+'): '+out.message); 
       sendEmailLog(EmailTemplate.LOG, { 
        message: '[CRON][Antpool][Account] Error response('+out.code+'): '+out.message 
       }); 
       return false; 
      } 

      // Add to database 
      let obj = { 
       earn24: out.data.earn24Hours, 
       earnTot: out.data.earnTotal, 
       paidOut: out.data.paidOut, 
       balance: out.data.balance, 
       createdAt: new Date() 
      }; 

      // Insert into database 
      let insert = await this.table.insert(obj).run(); 

      if(insert) { 
       return true; 
      } else { 
       return false; 
      } 

     }); 
    } 

} 

/** 
* Exports 
*/ 
export {Antpool}; 

我所得到的只是錯誤,有問題等待。

SyntaxError: .../antpool.js: Unexpected token (59:22) 

      // Insert into database 
      let insert = await this.table.insert(obj).run(); 

我在想什麼可以解決方案接受等待。這是不是很奇怪,因爲在代碼的其他部分等待運作良好。 不確定究竟是什麼問題,但花了大約兩天才發現問題。

我打電話的腳本有:

/** 
* Automatically hook babel into all node requires. 
*/ 
require('babel/register')({ 
    optional: ['es7.asyncFunctions', 'es7.classProperties', 'es7.decorators'] 
}); 

/** 
* Start application worker. 
*/ 
require('./src/worker'); 

任何幫助是非常讚賞。

+2

您正在使用的函數中等待「功能(ERR,HttpResponse對象,型)」未署名爲異步 –

回答

2

關鍵字await只能用於標記爲async的功能。功能您試圖使用await

, function (err, httpResponse, body) { ...

沒有被標記爲async,因此它不能被編譯。

更多信息:MDN article on async/await

+0

下一個問題就來了:執行:_cronsAntpool2 [「默認」]帳戶(。 'btc')無法讀取未定義的屬性'帳戶'這似乎是問題的地方hapi-cron-job和組合:執行:Antpool.account('btc') – Maximi

+2

這似乎是與您發佈的代碼無關的東西。但是,這似乎是一些功能異步回調的問題。當你意識到哪個函數被調用時,你可能會意識到它是'this'被設置爲'undefined'。然後你需要找到你傳遞迴調的地方,並用'.bind(具體對象)'將其綁定到某個具體對象。' 更多關於'.bind()': https://developer.mozilla.org/zh-cn/美國/文檔/網絡/的JavaScript /參考/ Global_Objects /功能/綁定 –