I have the same problem as this question but cant get the solution to work. I'm trying to write a nodejs script that will first check for a cached value in mongo but if it doesnt exist then go off and calculate it. The problem is that the findOne() callback doesnt get the original value of the loop variable so the callback cant correctly calculate and store the value. (I've omitted the actual fetching, storing and returning to focus on the actual issue I'm having). Whatever I do I cant get the original value to feed into the callback.
for (var d=start_date; d<now; d.setHours(d.getHours()+1)) {
(function(key) {
console.log('caller='+key)
db.collection('avgs').findOne( { date: key.toISOString() },function (err,data) {
console.log('callback='+key);
if (data) { //return data }
else { // compute average for given date , insert into database and return value }
});
})(d);
};
The console log shows this
caller=Tue Mar 25 2014 00:00:00 GMT+1030 (CST)
caller=Tue Mar 25 2014 01:00:00 GMT+1030 (CST)
caller=Tue Mar 25 2014 02:00:00 GMT+1030 (CST)
caller=Tue Mar 25 2014 03:00:00 GMT+1030 (CST)
callback=Wed Mar 26 2014 14:00:00 GMT+1030 (CST)
callback=Wed Mar 26 2014 14:00:00 GMT+1030 (CST)
callback=Wed Mar 26 2014 14:00:00 GMT+1030 (CST)
callback=Wed Mar 26 2014 14:00:00 GMT+1030 (CST)