Amit
Amit

Reputation: 3990

async nodejs querying and processing results

I have an array of objects taken from mongodb. Every element in the array is a post, with author as user_id. Now i wish to find the user info related to the user_id.

Since node uses async methods to find the data from db, the forEach loop finishes before the callbacks finish.

docs.forEach(function(doc, index){
        //get the user for this doc
        User.find({_id: mongo.BSONPure.ObjectID(doc.user_id)}, {name: 1, username: 1, email: 1}).skip(0).limit(1).toArray(function(err, user){
            user = user[0]
            if(err) {
                throw new Error(err)
            } else {
                docs[index].user = user
                if(doc.for_id) {
                    User.find({_id: mongo.BSONPure.ObjectID(doc.for_id)}, {name: 1, username: 1, email: 1}).skip(0).limit(1).toArray(function(err, for_user){
                        for_user = for_user[0]
                        if(err) {
                            throw new Error(err)
                        } else {
                            docs[index].for_user = for_user
                        }
                    })
                }
            }
        })
    })

So at the end of this loop, if i send a cb(docs), docs do not have the user and for_user attribute. How do I overcome this?

Upvotes: 3

Views: 2524

Answers (1)

mak
mak

Reputation: 13405

Use Step for node.js. It will run your functions in serial order

var Step = require('step');

Step(  docs.forEach(...), function() { cb(docs); } );

Or if you know the total number of records, you can call the callback when you're done processing the last one. Something like this

var count = docs.count(); // or something
var processed = 0;
docs.forEach(... if (++processed == count) cb(docs); );

Upvotes: 2

Related Questions