javascript - using variable outside of a function -



javascript - using variable outside of a function -

i start saying trying larn node having difficulties. building super simple weather app calls api , want homecoming data.

but not sure why wont homecoming info 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 + ' current tempature of ' + temp + '. feels ' +realfeel + ' high of '+max+' , low of '+min);

all variables local callback function. move console.log statement function.

if want variables global (bad idea), remove var statements callback function:

(function (){ // add together 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 + ' current tempature of ' + temp + '. feels ' +realfeel + ' high of '+max+' , low of '+min); } }); }()); // , line avoid global variables

but still need have piece of code display weather summary. console.log outside of callback function in post run before info comes forecast service. in short, must set console.log within callback function.

this reply straight straight reply specific question. please understand creating global variables anti-pattern.

javascript node.js

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -