0

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!

asked Mar 14, 2012 at 11:33
1
  • you should really considering using one of the flow control libraries (npm install seq will do), this code is hardly readable Commented Jul 4, 2012 at 0:01

1 Answer 1

2

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);
});
answered Mar 14, 2012 at 12:05
Sign up to request clarification or add additional context in comments.

1 Comment

yes it is like that but there is some mistake in my code that's why it is not working properly but after doing changes in that mistake it is working.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.