【发布时间】:2020-06-30 23:37:08
【问题描述】:
我试图从这个程序中获取天气 API 数据,我的问题是这个程序的输出是未定义的,我期望一个对象,为什么输出是未定义的以及如何获得所需的对象?
const url = "https://api.weatherapi.com/v1/current.json?key=KEYREDACTED&q=London"
const url1 = "https://api.openweathermap.org/data/2.5/weather?q=New Delhi,India&appid=KEYREDACTED"
const apiRequest = https.get(url,(res) => {
let data ="";
res.on("data",(chunk) => {
data+=chunk;
JSON.parse(data);
})
res.on("end",() => {
console.log(data.location);
});
});
【问题讨论】:
-
不是 node.js 方面的专家,但您不应该先检索所有数据然后才解析 JSON 吗?您提供的链接返回正确的数据。
标签: javascript node.js api http backend