简体   繁体   English

使用Node.JS和Heroku刷新api查询

[英]Refresh api query with Node.JS & Heroku

I'm building a messenger bot that queries an API once a day and sends the information contained there to users. 我正在构建一个Messenger机器人,该机器人每天查询一次API,并将其中包含的信息发送给用户。 it's hosted on Heroku and uses Node.js 它托管在Heroku上并使用Node.js

I'm currently using this to query the API: 我目前正在使用它来查询API:

var request = require('request');
//url for classes JSON
var url = 'https://example.api';

//get JSON, parse it and store it in jsonstorage variable
request(url, (error, response, body)=> {
    if (!error && response.statusCode === 200) {
    jsonstorage = JSON.parse(body)
    console.log("Got a response")
} else {
    console.log("Got an error: ", error, ", status code: ", response.statusCode)
}
})

The problem with this is that it doesn't refresh the API query ever, so the values returned are always the same. 问题在于它永远不会刷新API查询,因此返回的值始终相同。 If I try and put it within the functions that send the data once a day though, it claims the jsonstorage variable is 'undefined'. 但是,如果我尝试将其放在每天发送一次数据的函数中,则会声称jsonstorage变量为“未定义”。

How can I refresh the query regularly? 如何定期刷新查询?

Thanks! 谢谢!

我自己找到了答案–我遇到的问题是请求是异步的,因此我需要在运行其余脚本之前等待请求接收结果。

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

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