【发布时间】:2018-01-14 09:01:39
【问题描述】:
我有以下 Express 应用:
router.js
var express = require('express');
var router = express.Router();
var exch = require("../exchanges/exch")();
router.get('/', function(req, res, next) {
exch .getTicker((tick) => {
res.send('get ticker exch: ' + JSON.stringify(tick));
});
});
module.exports = router;
exch.js
var Kraken = require('kraken-api');
var moment = require('moment');
var _ = require('lodash');
/**
...code
**/
var Exchange = function(config) {
_.bindAll(this);
if(_.isObject(config)) {
this.key = config.key;
this.secret = config.secret;
this.currency = config.currency.toUpperCase()
this.asset = config.asset.toUpperCase();
}
this.pair = addPrefix(this.asset) + addPrefix(this.currency);
this.name = 'kraken';
this.since = null;
this.kraken = new Kraken(this.key, this.secret);
}
Exchange.prototype.getTicker = function(callback) {
var set = function(err, data) {
if(!err && _.isEmpty(data))
err = 'no data';
else if(!err && !_.isEmpty(data.error))
err = data.error;
if (err)
return console.log('unable to get ticker' + JSON.stringify(err));
var result = data.result[this.pair];
var ticker = {
ask: result.a[0],
bid: result.b[0]
};
callback(err, ticker);
};
this.kraken.api('Ticker', {pair: this.pair}, _.bind(set, this));
};
}
module.exports = Exchange;
如您所见,我通过require("...")() 包含该对象。但是,我仍然收到以下错误:
无法读取未定义的属性“getTicker”
TypeError:无法读取未定义的属性“getTicker” 在/home/ubuntu/workspace/routes/ticker.js:20:9 在 Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5) 在下一个(/home/ubuntu/workspace/node_modules/express/lib/router/route.js:137:13) 在 Route.dispatch (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:112:3) 在 Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5) 在/home/ubuntu/workspace/node_modules/express/lib/router/index.js:281:22 在 Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:335:12) 在下一个(/home/ubuntu/workspace/node_modules/express/lib/router/index.js:275:10) 在 Function.handle (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:174:3) 在路由器(/home/ubuntu/workspace/node_modules/express/lib/router/index.js:47:12)
为什么会出现这个错误,我该如何解决?
【问题讨论】:
标签: javascript node.js ecmascript-5