2017-11-11 226 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 /功能/绑定 –