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

async.forEach iteration

async.forEach iteration

Problem

I want data from multiple collection and then want to merge all data. i am using async module for sequential execution. i wrote code like

var newArray = new Array();
        common.findAllById('list',{'boardId': parseInt(bId)},function(err,result){                            
                if(err) console.log(err);
                else{                                                            
                    if(result != null){                          
                        async.forEachSeries(result, function (res, callback){                                
                            common.findAllById('cards',{'listId':parseInt(res.listId)},function(err,temp){
                                if(err){ callback("errr"+err);}
                                else{                                     
                                    var arr = new Array();
                                    async.forEachSeries(temp, function(tmp,callbackFn){
                                        common.findAllById('checklist',{'cardId':parseInt(tmp.cardId)}, function(err,r){
                                             if(err){ 
                                                 callbackFn("errr"+err);
                                             }
                                             else{
                                             if(r !== null){
                                                console.log("r:");

                                                tmp.checklists = r;                                                
                                                arr.push(tmp);                                                
                                                callbackFn(err);
                                             }
                                             }
                                        });                                        
                                    },function(err){ 
                                        console.log('arr'); 
                                        res.cards = arr; 
                                         console.log(res);
                                    });
                                    newArray.push(res);
                                    callback(err);
                                }                                                                
                            });                            
                        }, function (err){
                            console.log('after:');
                            console.log(newArray);                                                        
                            return response.send(newArray);
                        });                        
                    }
                }
            });

in this code after completing inner(second) Foreach Loop callback function for first foreach loop sholud called but after first iteration of second foreach loop first loop's callback is called and then it continues with second foreach loop iteration. what is mistake in this code please help me. thank you in advance!

Problem courtesy of: priyanka patel

Solution

You should call callback() of first forEachSeries into the the final callback of the inner forEachSeries.

},function(err){ 
    console.log('arr'); 
    res.cards = arr; 
    console.log(res);
    newArray.push(res);
    callback(err);
});
Solution courtesy of: SeVeNDuS

Discussion

View additional discussion.



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

Share the post

async.forEach iteration

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×