如何使minnStats在req.end函数之外工作。我执行了来自unirest的get调用,并且想要导出数据。我不确定如何在req.end函数之外使用'covidData'参数。如何使底部的console.log正常工作?
var unirest = require("unirest");
var req = unirest("GET", "https://covid-19-coronavirus-statistics.p.rapidapi.com/v1/stats");
req.query({
"country": "US"
});
req.headers({
"x-rapidapi-host": "covid-19-coronavirus-statistics.p.rapidapi.com",
"x-rapidapi-key": "2e6a00a0b7mshb40a079e7a67f38p181597jsn5910590141c3"
});
req.end(function (covidData) {
if (covidData.error) {
throw new Error(covidData.error)
} else {
var covidStats = covidData.body.data.covid19Stats
var minnStats = covidStats.filter(stat => stat.province === 'Minnesota')
module.exports = minnStats
console.table(minnStats)
console.table(covidData.body)
return covidData;
}
})
console.log(minnStats)
答案 0 :(得分:0)
我在函数中编写了jquery代码,然后使用了“ browserify”模块,因此无论“ require(unirest)”如何运行,js都能在我的浏览器上运行。