I stumbled upon a problem in mongoose recently, here's my code:
var v, query;
for(var i=0; i<values.length; i++) {
v = values[i];
query = model.findOne({type:v});
query.exec(function(err, doc) {
if(doc == undefined) {
var m = new model({type:v, count:0});
// but the 'v' above is not the values[i] in the for loop
// code to save comes here
} else {
doc.count++;
// update code comes here
}
});
}
I wanted to check if doc was null, and if so, enter a default value into the database. And if there was a doc returned, then update it's properties. The problem is that the object I'm trying to save has values[i] as one of it's properties. But since it's a callback, I do not get that particular value as it is in the for loop carries onward.
I got around this by inserting a default object for all the different values during model creation itself, but is there a way to do this at this point in the code flow?
Try this instead:
values.forEach(function(v) {
var query = model.findOne({type:v});
query.exec(function(err, doc) {
if(doc == undefined) {
var m = new model({type:v, count:0});
// but the 'v' above is not the values[i] in the for loop
// code to save comes here
} else {
doc.count++;
// update code comes here
}
});
});
The reason it doesn't work with a for loop is because Javascript doesn't have a block scope, meaning that the v variable referenced in the block is reused instead of recreated.
When you use the variable immediately (like with model.findOne({ type: v}) that's not a problem, but since the callback function for query.exec will (probably) execute after the loop has finished, the v variable in the callback will contain only the last value of v in the loop.
By using forEach, you do create a new v variable every time.