Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Mongoose : Trying to create a document in a callback, with values in an outer loop

Mongoose : Trying to create a document in a callback, with values in an outer loop

Problem

I stumbled upon a problem in mongoose recently, here's my code:

var v, query;
for(var i=0; i

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?

Problem courtesy of: Raghuveer

Solution

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.

Solution courtesy of: robertklep

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Mongoose : Trying to create a document in a callback, with values in an outer loop

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×