Node JS 为 API 调用返回 undefined
Node JS returning undefined for an API call
我正在使用开放天气 API 并且风速返回未定义但其他两个(时间和温度)工作正常。
var request = require('request');
var yargs = require('yargs');
var fs = require ('fs');
var forecasts = require('./forecast.json');
var args = yargs.argv;
var url = 'http://api.openweathermap.org/data/2.5/forecast?q=erie,pa&APPID=a0472e6063198c88952f86f836355ae1&units=imperial';
request({url: url, json: true},function(error,response,body){
var time = 0;
body.list.forEach(function(measurement){
var forecast = {Wind_Speed: measurement.main.wind, time: time, temp: measurement.main.temp};
forecasts.push(forecast);
time = time + 3
console.log(forecast)
})
fs.writeFile('forecast.json',JSON.stringify(forecasts))
})
我刚刚检查了 API 看起来风不在主要对象下你应该直接从测量中获取它:
var forecast = {
Wind_Speed: measurement.wind.speed,
time: time,
temp: measurement.main.temp
};
格式化API响应:
我正在使用开放天气 API 并且风速返回未定义但其他两个(时间和温度)工作正常。
var request = require('request');
var yargs = require('yargs');
var fs = require ('fs');
var forecasts = require('./forecast.json');
var args = yargs.argv;
var url = 'http://api.openweathermap.org/data/2.5/forecast?q=erie,pa&APPID=a0472e6063198c88952f86f836355ae1&units=imperial';
request({url: url, json: true},function(error,response,body){
var time = 0;
body.list.forEach(function(measurement){
var forecast = {Wind_Speed: measurement.main.wind, time: time, temp: measurement.main.temp};
forecasts.push(forecast);
time = time + 3
console.log(forecast)
})
fs.writeFile('forecast.json',JSON.stringify(forecasts))
})
我刚刚检查了 API 看起来风不在主要对象下你应该直接从测量中获取它:
var forecast = {
Wind_Speed: measurement.wind.speed,
time: time,
temp: measurement.main.temp
};
格式化API响应: