Sunday, 16 April 2017

How to assign global variable inside connection query?

I execute several SQL queries to get the new data from other tables. Each of the execution is running asynchronous function. Global variable or outside that function can't assign new value within the function. How do I assign global variable inside connection query ?

Code

var user_id = -1;

function setUserId(id){
  user_id = id;
}

connection.query('SELECT id FROM user' , function(err, result){
   setUserId(result[0].id);
});

return res.json({
   user_id: user_id
});

Output

user_id: -1

Expected

user_id: 1



via Foonation

No comments:

Post a Comment