I have the following Express app:
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;
As you can see I am including the object via require("...")()
. However, I still get the following error:
Cannot read property 'getTicker' of undefined
TypeError: Cannot read property 'getTicker' of undefined at /home/ubuntu/workspace/routes/ticker.js:20:9 at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5) at next (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5) at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:335:12) at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:275:10) at Function.handle (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:174:3) at router (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:47:12)
Why is this error occurring and how can I fix it?