简体   繁体   English

从API获取数据并在快速视图中输出

[英]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. 我已经编写了以下API包装器,并希望在快速视图中将其输出。

My API calling file is exchanges.js and I use the following function to getQuotes : 我的API调用文件是exchanges.js,我使用以下函数进行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 : 我正在尝试通过快速路线ticker.js调用getQuotes

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 无法读取未定义的属性“ getQuote”

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) TypeError:无法在Layer.handle的/home/ubuntu/workspace/nodejs/routes/ticker.js:7:31读取未定义的属性'getQuote'[作为handle_request](/ home / ubuntu / workspace / nodejs / node_modules / express /lib/router/layer.js:95:5)在Route.dispatch(/ home / ubuntu)下(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/route.js:137:13) /workspace/nodejs/node_modules/express/lib/router/route.js:112:3)在Layer.handle [作为handle_request](/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/layer.js :95:5)在/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:281:22在Function.process_params(/ home / ubuntu / workspace / nodejs / node_modules / express / lib /下一个(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:275:10)在Function.handle(/ home / ubuntu / workspace /路由器(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:47:12)上的nodejs / node_modules / express / lib / router / index.js:174:3)

Any suggestion how to call the getQuotes variable correctly? 任何建议如何正确调用getQuotes变量?

Appreciate your reply! 感谢您的回覆!

In order to import it and use getQuote you will need to include it like this var kraken = require("./kraken")() 为了导入它并使用getQuote您需要像这样包含它var kraken = require("./kraken")()

then you will be able to to call getQuote buy invoking var tick = kraken.getQuote(); 那么您将可以调用getQuote购买,调用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 编辑:我一直在本地工作,所以我的路径是./kraken您仍应使用默认路径来归档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM