0

I am working with NodeJS and MongoDB and on start I create collections and fill them in if they don't already exist. Right now this is just development , but with backups and things I could end up doing the same thing or something similar in production.

My question is what will happen to the iteration variable(i)? Will the callback use the correct value for that iteration? Or will it get some other value further down the list, or perhaps none at all?

ldb.createCollection('people', { strict: true }, function(err, col){
    if(err) { console.log('createCollection( people ) ', err); } 
    else { 
        for(var i in people){
            col.insert(people[i], { w: 1 }, function(err, doc){
                people[i]._id = doc._id;
            }
        }
    }
});

Edit: This is an object, not an array.

1

1 Answer 1

1

When the callback passed into insert is called, the value of i will not be preserved (it will most likely be the last key in people). If you want to preserve i, you could give it its own functional scope like this:

people.forEach(function(person, i) {
    col.insert(person, { w: 1 }, function(err, doc) {
        person._id = doc._id;
    });
});

edit: Using an anonymous function instead of the forEach method:

for(var i in people) {
    function(i) {
        col.insert(people[i], { w: 1 }, function(err, doc){
            people[i]._id = doc._id;
        });
    }(i);
}
Sign up to request clarification or add additional context in comments.

4 Comments

What about (function(var1, var2){ /* do something */ }(this.var1, this.var2);? Seems like I've seen that used in cases? How would that be done? Would it work? This is an object, not an array, so it doesn't have a foreach function.
Yes. That would probably be a more precise solution to your problem. I will add an example of that to my answer.
The only reason I would suggest using a forEach call is it's cleaner and avoids common issues associated with for..in loops (such as requiring the use of the hasOwnProperty method).
my $0.02: i find it easer to scan when you use a different variable name inside the wrapper than out of it. repeating "i" works and preserves the orig, but it can be confusing...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.