I'm using express, node, and handlebars. I have this bit of code for handling POSTs. If the user clicks the "add item" button, it takes their entry for a city, looks up the weather for that city using the Open Weather Map API, and changes the color around the background of that city item based on the weather.
app.post('/',function(req,res){
var context = {};
var temperature;
if(req.body['Add Item']){
req.session.toDo.push({"name":req.body.name, "id":req.session.curId, "location":req.body.city});
req.session.curId++;
request('http://api.openweathermap.org/data/2.5/weather?q=' + req.body.city + '&units=imperial&APPID=' + credentials.owmKey, handleGet);
function handleGet(err, response, body){
if(!err && response.statusCode < 400){
context.owm = body;
var newText = JSON.parse(context.owm);
temperature = newText.main.temp;
}
}
}
...
if(temperature > 70) {
context.script = "<script>document.getElementById('owm').style.backgroundColor='LightGreen';</script>"
}
else if (temperature < 70){
context.script = "<script>document.getElementById('owm').style.backgroundColor='LightPink';</script>"
}
}
However, the temperature variable is returning 'not defined'. I figure this has to do something with asynchronicity, but I'm not sure what I should do to correct it.