Carol.Kar
Carol.Kar

Reputation: 5345

Get Data from API and output it in express view

I have written the following API wrapper and would like to output it in an express view.

My API calling file is exchanges.js and I use the following function to getQuotes:

module.exports = function container(get, set, clear) {

  function publicClient() {
    if (!public_client) {
      public_client = new KrakenClient()
    }
    return public_client
  }


  var exchange = {
    name: 'kraken',
    historyScan: 'forward',
    makerFee: 0.16,
    takerFee: 0.26,
    // The limit for the public API is not documented, 1750 ms between getTrades in backfilling seems to do the trick to omit warning messages.
    backfillRateLimit: 1750,

    getQuote: function(opts, cb) {
      var args = [].slice.call(arguments)
      var client = publicClient()
      var pair = joinProduct(opts.product_id)
      client.api('Ticker', {
        pair: pair
      }, function(error, data) {
        if (error) {
          if (error.message.match(recoverableErrors)) {
            return retry('getQuote', args, error)
          }
          console.error(('\ngetQuote error:').red)
          console.error(error)
          return cb(error)
        }
        if (data.error.length) {
          return cb(data.error.join(','))
        }
        cb(null, {
          bid: data.result[pair].b[0],
          ask: data.result[pair].a[0],
        })
      })
    },

  }
  return exchange
}

I am trying to call getQuotes via an express route - ticker.js:

var express = require('express');
var router = express.Router();
var kraken = require("../exchanges/kraken/exchange")

router.get('/', function(req, res, next) {
    var tick = kraken.exchange.getQuote;
    res.send('get ticker: ' + tick);
});

module.exports = router;

However, when opening the route I get the following error message:

Cannot read property 'getQuote' of undefined

TypeError: Cannot read property 'getQuote' of undefined at /home/ubuntu/workspace/nodejs/routes/ticker.js:7:31 at Layer.handle [as handle_request] (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/layer.js:95:5) at next (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/layer.js:95:5) at /home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:335:12) at next (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:275:10) at Function.handle (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:174:3) at router (/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:47:12)

Any suggestion how to call the getQuotes variable correctly?

Appreciate your reply!

Upvotes: 0

Views: 537

Answers (1)

Kukic Vladimir
Kukic Vladimir

Reputation: 1010

In order to import it and use getQuote you will need to include it like this var kraken = require("./kraken")()

then you will be able to to call getQuote buy invoking var tick = kraken.getQuote();

Here is a working example

var express = require('express');
var app     = express();
var kraken = require("./kraken")()

app.get('/', function(req, res, next) {
  var tick = kraken.getQuote();
  res.send('get ticker: ' + tick);
});


app.listen(3000, function() {
  console.log('Example app listening on port 3000!')
});

Edit: I have been working localy so my path is ./kraken you should still use your default path to file

Upvotes: 1

Related Questions