You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to run a query in an eachSeries. The problem is that doneCallback gets called before the query's callback is complete. How can I fix this?
vardataset=newArray("1","2");varallVar1=newArray();async.eachSeries(dataset,function(set,doneCallback){varselectallquery='SELECT * FROM `data'+set+'` ORDER BY id DESC LIMIT 1';mysqlcon.query(selectallquery,function(err,rows,fields){if(err)throwerr;allVar1.push(rows[0].var1);// This ends up happening after or during the "Do Afterwards."});returndoneCallback(null);// Fires prematurely},function(err){// Do afterwardsconsole.log(allVar1);});
Thanks!
The text was updated successfully, but these errors were encountered:
Just like any code that uses callbacks, your doneCallback neds to be within the callbck from the query method:
vardataset=newArray("1","2");varallVar1=newArray();async.eachSeries(dataset,function(set,doneCallback){varselectallquery='SELECT * FROM `data'+set+'` ORDER BY id DESC LIMIT 1';mysqlcon.query(selectallquery,function(err,rows,fields){//if (err) throw err; // <-- Do not throw errors in callbacksif(err)returndoneCallback(err);// <-- This is what the first argument to doneCallback is forallVar1.push(rows[0].var1);// This ends up happening after or during the "Do Afterwards."doneCallback(null);// <-- Place here});//return doneCallback(null); // <-- No, not here},function(err){// Do afterwardsconsole.log(allVar1);});
I am trying to run a query in an
eachSeries
. The problem is thatdoneCallback
gets called before the query's callback is complete. How can I fix this?Thanks!
The text was updated successfully, but these errors were encountered: