title: Cannot read property of undefined when using exported function
body: <p>I have the following Express app:</p>
  
  <p><strong>router.js</strong></p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var exch = require("../exchanges/exch")();
  
  router.get('/', function(req, res, next) {
  
      exch .getTicker((tick) =&gt; {
          res.send('get ticker exch: ' + JSON.stringify(tick));
      });
  
  });
  
  module.exports = router;
  </code></pre>
  
  <p><strong>exch.js</strong></p>
  
  <pre><code>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 &amp;&amp; _.isEmpty(data))
        err = 'no data';
  
      else if(!err &amp;&amp; !_.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;
  </code></pre>
  
  <p>As you can see I am including the object via <code>require("...")()</code>. However, I still get the following error:</p>
  
  <blockquote>
    <p>Cannot read property 'getTicker' of undefined</p>
    
    <p>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)</p>
  </blockquote>
  
  <p>Why is this error occurring and how can I fix it?</p>