【问题标题】:Why is my finnhub.io request returning same response each time?为什么我的 finnhub.io 请求每次都返回相同的响应?
【发布时间】:2023-02-25 08:14:15
【问题描述】:

我每秒发出一个股票价格的 API 请求,每次都得到相同的响应。当我刷新页面时,我得到了一个新的响应,但是无论我重复调用多少次,它都没有改变。

使用“finnhub”时会发生这种情况 但是每次使用polygon.io api 时它都会改变。 然而,polygon.io 给我延迟 15 分钟的数据,但无法正常工作。

为了发出请求,我使用了 axios node.js 包。

ApiRequest = function () {
  const currentPriceURL = `https://finnhub.io/api/v1/quote?symbol=SPY&token=` + API_KEY;

  axios.get(currentPriceURL)
    .then(response => {

      // record price of SPY
      var price = response.data.c;
      console.log("price= " + price);
    }).catch(error => console.error(`Error: ` + error));
} // end of ApiRequest function

// repeat every second
let DisplaySpyPrice_Timer = setInterval(ApiRequest, 1000);

为什么一个 API 需要页面刷新才能更新,而另一个则不需要?

【问题讨论】:

  • 页面刷新更新?这不是 node.js 脚本吗?

标签: javascript node.js api axios finnhub


【解决方案1】:

不是你的代码问题,polygon.io没有更新数据。

我用切换开关符号测试了你的代码。 每次都是变价。

const axios = require('axios')

const API_KEY = '<your API Key>'
let even = true
ApiRequest = function () {
    const stockSymbol = (even) ? 'SPY' : 'AAPL'
    const currentPriceURL = `https://finnhub.io/api/v1/quote?symbol=${stockSymbol}&token=` + API_KEY;
    even = !even;

    axios.get(currentPriceURL)
        .then(response => {

            // record price of SPY
            var price = response.data.c;
            console.log("price= " + price);
        }).catch(error => console.error(`Error: ` + error));
} // end of ApiRequest function

// repeat every second
let DisplaySpyPrice_Timer = setInterval(ApiRequest, 1000);

结果

$node get-stock-price.js
price= 396.38
price= 146.71
price= 396.38
price= 146.71
price= 396.38
price= 146.71

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多