All right, I think it's easier if I just copy my whole node.js route so you can see what I'm talking about.
I'm trying to make multiple queries to my MongoDB database (one for each day). The queries are running fine, but by the time they fire their callbacks, my incrementor variable has already incremented.
So for this:
exports.graph = function(req, res) {
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
database.collection('customerData', function(err, collection) {
for (var i = 2; i < 18; i++) {
sDay = pad(i, 2);
eDay = pad(i + 1, 2);
collection.find({
DATE: {
$gte: sDay + 'APR13:00:00:00',
$lt: eDay + 'APR13:00:00:00'
}
}, function(err, cursor){
cursor.toArray(function(err, data){
var counter = 0;
for (var point in data) {
trans = parseInt(data[point].Total_Transaction * 100);
counter += trans;
}
console.log(i, counter / 100);
});
});
}
});
}
I get an output of this:
18 22023.29
18 24483.03
18 22644.11
18 23194.31
18 21560.99
18 23024.32
18 24384.93
18 23138.34
18 24400.63
18 28418.6
18 31691.65
18 31111.62
18 42358.74
18 38355.76
18 36787.52
18 42870.19
18 22023.29
18 22644.11
18 24483.03
18 23194.31
18 21560.99
18 23024.32
18 24400.63
18 23138.34
18 24384.93
18 28418.6
18 31691.65
18 31111.62
18 42358.74
18 38355.76
18 36787.52
18 42870.19
This (obviously) isn't ideal- the numbers aren't necessarily coming back in the order the queries are being fired, so it's important to me to figure out which ones go where.
I can't figure out a way to store the results of my arithmetic in the query callback in a meaningful way so I can use it later.
Any thoughts?