如何用Babel调用函数

时间:2017-11-11 11:29:04

标签: node.js babel hapijs

我尝试用hapi-cron-job调用函数,但我总是得到错误:

  

worker.js:54执行:_cronsAntpool2 ['default']。account('btc'),   TypeError:无法读取未定义的属性“account”

我的worker.js有这段代码:

/**
 * Imports
 */
import Hapi from 'hapi';
import schedule from 'hapi-cron-job';
import config from './config';
import Antpool from './crons/antpool';

/**
 * Server setup
 */
const server = new Hapi.Server({
    connections: {
        router: {
            stripTrailingSlash: true
        }
    }
});

server.connection({
    host: config.app.workerhost,
    port: config.app.workerport,
    routes: {
        cors: {
            additionalHeaders: [
                'Origin'
            ]
        }
    }
});

server.register({
    register:require('hapi-cron-job'),
    options:{
        localTime: false, //GMT
        jobs:[
            {
                name: "antpool - account - btc",
                enabled: true,
                immediate: true,
                schedule: "every 24 hours",
                execute: Antpool.account('btc'),
                environments: ['development','production']
            }
        ],
        callback: callback
    }
}, function(err){
    if(err) { throw err; }
});

该函数,名为:

/**
 * 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)
    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 = this.table.insert(obj).run();

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

        });
    }


}

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

我正在尝试找到正确的电话。但总是得到错误。

在worker.js中看似行执行问题:Antpool.account('btc')。

任何想法如何正确的通话功能对我有帮助。 我正在使用Babel v.5.8.38

0 个答案:

没有答案