javascript - node.js async each function callback failing -


i'm making call db, , want use results fire off request server, , handle responses. want asynchronously trying use async library.

var sendrequest = function (store) {      console.log(store); }  connection.connect();  connection.query('select * stores', function(err, rows, fields) {     if (!err){         async.each(rows, function(row, sendrequest) {             var store = {id: row.id, address: row.address};             //console.log(store); <-- works             sendrequest(store); <-- logs {id: '', address: null} once         }, function(err){             console.log(err);         });     }     else         console.log('error performing query'); });  connection.end(); 

if log store variable directly, logs each instance in array rows , properly. if try through sendrequest callback, once , improperly.

you confused things. you're using same name, sendrequest, each callback. should instead:

connection.query('select * stores', function(err, rows, fields) {   if (!err){     async.each(rows, function(row, callback) {         var store = {id: row.id, address: row.address};         sendrequest(store);         callback();      }, function(err){         console.log(err);     });   }   else     console.log('error performing query'); }); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -