简体   繁体   中英

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. it's hosted on Heroku and uses Node.js

I'm currently using this to query the 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. If I try and put it within the functions that send the data once a day though, it claims the jsonstorage variable is 'undefined'.

How can I refresh the query regularly?

Thanks!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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