priyanka patel
priyanka patel

Reputation: 637

async.forEach iteration

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!

Upvotes: 0

Views: 2007

Answers (1)

SeVeNDuS
SeVeNDuS

Reputation: 298

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);
});

Upvotes: 2

Related Questions