2016-04-11 59 views
0

我正在使用開放天氣API和風速它返回未定義,但其他兩個(時間和溫度)正常工作。節點JS返回未定義的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)) 
}) 

回答

0

我剛查了API,它看起來像風是不是下的主要對象,你應該把它從直接測量:

var forecast = { 
    Wind_Speed: measurement.wind.speed, 
    time: time, 
    temp: measurement.main.temp 
}; 

格式化API響應:

enter image description here

+0

真棒!謝謝 – Thesystem32