Reputation: 2245
I will start by saying that i am really trying to learn node but having some difficulties. I am building a super simple Weather App that calls an API and then i want to return the data.
But i am not sure why it wont return this data to the console?
var request = require('request');
request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
if (!error && response.statusCode == 200) {
//var info = JSON.parse(body);
var jsonObject = JSON.parse(body);
var summary = jsonObject.currently.summary;
var temp = jsonObject.currently.temperature;
var realFeel = jsonObject.currently.apparentTemperature;
var summary2 = jsonObject.hourly.summary;
var max = jsonObject.daily.data[0].temperatureMax;
var min = jsonObject.daily.data[0].temperatureMin;
}
})
console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
Upvotes: 0
Views: 99
Reputation: 8333
All your variables are local to the callback function. Move your console.log statement into the function.
If you want all your variables to be global (bad idea), remove the var
statements from the callback function:
(function (){ // add this line
var request = require('request');
var summary, temp, realFeel, summary2, max, min;
request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
if (!error && response.statusCode == 200) {
//var info = JSON.parse(body);
var jsonObject = JSON.parse(body);
summary = jsonObject.currently.summary;
temp = jsonObject.currently.temperature;
realFeel = jsonObject.currently.apparentTemperature;
summary2 = jsonObject.hourly.summary;
max = jsonObject.daily.data[0].temperatureMax;
min = jsonObject.daily.data[0].temperatureMin;
console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
}
});
}()); // and this line to avoid global variables
But then you still need to have a piece of code to display your weather summary. The console.log outside of the callback function in your post will run before the data comes back from the forecast service. In short, you MUST put the console.log inside the callback function.
This answer directly is a straight reply to your specific question. Please understand that creating global variables is an anti-pattern.
Upvotes: 0
Reputation: 38849
Node is an asynchronous server. When you execute a request that is going to block, the node engine continues executing the following code. You're passing the request module a callback, but your console.log is being executed immediately after the request is fired off and before and results have returned.
What you want to do is put your code where you use the data inside the callback function.
var request = require('request');
request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
if (!error && response.statusCode == 200) {
//var info = JSON.parse(body);
var jsonObject = JSON.parse(body);
var summary = jsonObject.currently.summary;
var temp = jsonObject.currently.temperature;
var realFeel = jsonObject.currently.apparentTemperature;
var summary2 = jsonObject.hourly.summary;
var max = jsonObject.daily.data[0].temperatureMax;
var min = jsonObject.daily.data[0].temperatureMin;
console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
}
})
You should also not ignore errors.
var request = require('request');
request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
if (error) throw error;
if (response.statusCode != 200) return console.log('Invalid status code: '+response.statusCode)
var jsonObject = JSON.parse(body);
var summary = jsonObject.currently.summary;
var temp = jsonObject.currently.temperature;
var realFeel = jsonObject.currently.apparentTemperature;
var summary2 = jsonObject.hourly.summary;
var max = jsonObject.daily.data[0].temperatureMax;
var min = jsonObject.daily.data[0].temperatureMin;
console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
});
Upvotes: 3
Reputation: 1423
The problem is that when you declare a variable with var
you are declaring it to be scoped to the function you are in. If you use a name without declaring it with var it will look them up in the global "window" scope.
Drop the var keywords in the callback function and the function should mutate the global scope.
Upvotes: 0