I'm making a call to my db, and want to use the results to fire off a request to another server, and then handle the responses. I want to do this asynchronously so I am trying to use the async library.
var sendRequest = function (store) {
console.log(store);
}
connection.connect();
connection.query('SELECT * from stores', function(err, rows, fields) {
if (!err){
async.each(rows, function(row, sendRequest) {
var store = {id: row.id, address: row.address};
//console.log(store); <-- this works
sendRequest(store); <-- this logs {id: '', address: null} only once
}, function(err){
console.log(err);
});
}
else
console.log('Error performing query');
});
connection.end();
If I log the store variable directly, it logs for each instance in the array rows and properly. If I try to do it through the sendRequest callback, it does it only once and improperly.
sendRequest. It can cause potential confusion making people think thesendRequestinside theasync.eachcallback have anything to do with thesendRequestfunction at the top.