I would like to know how can I save globally a variable inside the request?
In this case, I am doing multiple requests and I increment every time. I want to use increment variable, outside the request code.
This is my code:
var url ="";
for (i = 0; i <= queries.startDates.length - 2; i++)
{
var increment;
//console.log(queries.endDates[i]);
url = `https://api.weatherbit.io/v2.0/history/daily?city=${city}&start_date=${queries.startDates[i]}&end_date=${queries.endDates[i]}&key=${historicalApiKey}`;
// console.log(url);
request(url, function(err, response, body)
{
// On return, check the json data fetched
if (err)
{
res.render('HistoricalData', { weather: null, error: 'Error, please try again' });
}
else
{
//console.log(body)
let weather = JSON.parse(body);
//console.log(weather);
if (weather == undefined)
{
//console.log("ana");
res.render('HistoricalData', { weather: null, error: 'Error, please try again' });
}
else
{
var weeklyInfo = structureData(loopThroughDays(weather),organizedTemperaturesMap);
//console.log(weeklyInfo.get('Oct'));
increment++;
}
}
});
console.log(increment);
}
In this case, increment will be 0. How can I actually do it? Thank you