Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从API获取实时数据到路由_Javascript_Node.js_Ccxt - Fatal编程技术网

Javascript 从API获取实时数据到路由

Javascript 从API获取实时数据到路由,javascript,node.js,ccxt,Javascript,Node.js,Ccxt,我使用以下两个文件从两个API获取数据。请在下面找到我的最低可行示例: poloniex.js const Poloniex = require('poloniex-api-node') const poloniex = new Poloniex() async function getExchangeTicker() { poloniex.returnTicker((err, ticker) => { if (err) { console.log(err.mes

我使用以下两个文件从两个API获取数据。请在下面找到我的最低可行示例:

poloniex.js

const Poloniex = require('poloniex-api-node')
const poloniex = new Poloniex()

async function getExchangeTicker() {
  poloniex.returnTicker((err, ticker) => {
    if (err) {
      console.log(err.message)
    } else {
      //console.log(ticker)
      return ticker
    }
  })
}

module.exports = {
  getExchangeTicker,
}
const ccxt = require ('ccxt')

async function getExchangeTicker() {
  const bitfinex = new ccxt.bitfinex({ verbose: true })  
  const data = await bitfinex.fetchTicker()
  return data
}

module.exports = {
  getExchangeTicker,
}
const exchangePoloniex = require('../exchange/poloniex')
const exchangeCCTX = require('../exchange/cctx')

async function getAllTickers() {
  const exchanges = [
    exchangePoloniex,
    exchangeCCTX,
  ]

  let res
  exchanges.forEach((exchange) => {
    res = exchange.getExchangeTicker()
  })
  return res
}

async function runScheduler() {
  let res
  setInterval(() => {
    this.res = getAllTickers()
  }, 3000)
  console.log("res: " + res)
  return res
}

runScheduler()
cctx.js

const Poloniex = require('poloniex-api-node')
const poloniex = new Poloniex()

async function getExchangeTicker() {
  poloniex.returnTicker((err, ticker) => {
    if (err) {
      console.log(err.message)
    } else {
      //console.log(ticker)
      return ticker
    }
  })
}

module.exports = {
  getExchangeTicker,
}
const ccxt = require ('ccxt')

async function getExchangeTicker() {
  const bitfinex = new ccxt.bitfinex({ verbose: true })  
  const data = await bitfinex.fetchTicker()
  return data
}

module.exports = {
  getExchangeTicker,
}
const exchangePoloniex = require('../exchange/poloniex')
const exchangeCCTX = require('../exchange/cctx')

async function getAllTickers() {
  const exchanges = [
    exchangePoloniex,
    exchangeCCTX,
  ]

  let res
  exchanges.forEach((exchange) => {
    res = exchange.getExchangeTicker()
  })
  return res
}

async function runScheduler() {
  let res
  setInterval(() => {
    this.res = getAllTickers()
  }, 3000)
  console.log("res: " + res)
  return res
}

runScheduler()
scheduler.js

const Poloniex = require('poloniex-api-node')
const poloniex = new Poloniex()

async function getExchangeTicker() {
  poloniex.returnTicker((err, ticker) => {
    if (err) {
      console.log(err.message)
    } else {
      //console.log(ticker)
      return ticker
    }
  })
}

module.exports = {
  getExchangeTicker,
}
const ccxt = require ('ccxt')

async function getExchangeTicker() {
  const bitfinex = new ccxt.bitfinex({ verbose: true })  
  const data = await bitfinex.fetchTicker()
  return data
}

module.exports = {
  getExchangeTicker,
}
const exchangePoloniex = require('../exchange/poloniex')
const exchangeCCTX = require('../exchange/cctx')

async function getAllTickers() {
  const exchanges = [
    exchangePoloniex,
    exchangeCCTX,
  ]

  let res
  exchanges.forEach((exchange) => {
    res = exchange.getExchangeTicker()
  })
  return res
}

async function runScheduler() {
  let res
  setInterval(() => {
    this.res = getAllTickers()
  }, 3000)
  console.log("res: " + res)
  return res
}

runScheduler()
我正在运行一个调度程序来汇集这些文件中的数据,但只返回
res:undefined

对于如何正确地从这两个API获取数据,有什么建议吗


非常感谢您的回复

我不知道您正在访问的两个API,因此我无法真正判断您从poloniex.js或cctx.js获得的代码是否良好-我假设您可以通过console.logs等来判断,您分别从每个API获得了所需的API数据

但我可以在scheduler.js文件中看到一些逻辑问题:

  • getAllTickers
    中,您的
    .forEach
    循环每次迭代都会覆盖
    res
    ,因此只有最后的结果才可见
  • 由于它是一个
    async
    函数,
    getAllTickers
    返回一个承诺。您需要使用
    。然后在它返回的内容上使用
    或类似的内容
  • console.log并在
    runScheduler
    中返回,在setInterval有机会执行之前完成,即使执行一次,因此那里没有可用的数据
  • 我认为以下设置可能是您想要的:

    const exchangePoloniex = require('../exchange/poloniex')
    const exchangeCCTX = require('../exchange/cctx')
    
    async function getAllTickers() {
    
      let updatedTickers = []
      updatedTickers[0] = await exchangePoloniex.getExchangeTicker()
      updatedTickers[1] = await exchangeCCTX.getExchangeTicker()
    
      return updatedTickers
    }
    
    function runScheduler() {
      let tickers
      setInterval(() => {
        tickers = getAllTickers()
        console.log(tickers) // tickers is a promise at this point
    
        tickers.then((data) => {
            console.log(data) // should be the data returned from your apis
            // store the data in your db
        })
      }, 3000)
    }
    
    runScheduler()
    
    请注意,
    runScheduler
    不必是异步的,因为您没有对返回值执行任何操作-所有工作都在
    setInterval
    回调内


    如果您需要响应浏览器请求来提供此数据,则可以从数据库中获取该数据,知道它已在过去3秒内更新。

    runScheduler
    中,您的console.log在第一次
    setInterval
    之前以3秒的速度运行。也许
    res
    正在被填充,而你只是不知道而已。你打算用它做什么?还有,你的分号都在哪里??它可能会运行,但我觉得它是在自找麻烦,把它们都排除在外@我基本上想要这两个API的数据。关于
    ->
    eslint airbnb
    我想setInterval是我得不到的-您想要API的数据来响应浏览器请求吗?存储在内存中的变量?用于更新数据库…?@arbuthnott基本上我想在数据库中更新它,并在浏览器请求中使用它