関数getweather()では、Json オブジェクトから気象データを取得し、そのデータを変数dataに格納します。その関数内で Json データを処理できますが、getweather()の外部からデータにアクセスするにはどうすればよいでしょうか?
変数locationまたはdata を返すかどうかは問題ではありません。変数の場所は Json ではありません。
関数getweather()内で動作するように、プレース変数を処理するにはどうすればよいですか?
var ajax = require('ajax');
var place;
place = getweather();
function getweather()
{
// Make the request
ajax(
{
url: 'http://api.openweathermap.org/data/2.5/weather?q=paris.fr', type: 'json'
},
function(data)
{
// Success!
console.log("Successfully fetched weather data!");
// Extract data
var location = data.name;
return location;
},
function(error) {
// Failure!
console.log('Failed fetching weather data: ' + error);
}
);
}