I'm pretty new to Node.JS and I'm trying to build an app but I'm running into some issues which I can't seem to find a solution to. I spent quite a bit of time on Google with no luck.
Below is a little snippet which illustrate what I'm trying todo.
mysql.query('SELECT * FROM leads', function(err, res){
for(x in res){
id = res[x];
mysql.query('SELECT FROM survey WHERE lead_id = ?', [id], function(x, res){
// I want to access the id variable here but since this is in a
// callback the id variable could've been changed by now
// AND THIS DOES'T ALWAYS RETURN SOMETHING OR I COULD'VE
// JUST USED THE LEAD_ID IN THE RETURNED DATASET
});
}
});
I'd like to access that id variable within the callback but due to the async nature of node the variable would've changed since the for loop finish executing before the first callback is even returned.
My question is can you pass the variable through to the the callback function? Or can someone help me with another solution.
Thanks in advance!
Ahmad