I don’t know javascript very well. I can’t define a global variable.
var data = 'empty';
connection.query('SELECT * FROM deneme',function(err, rows, fields){
if (err) throw err;
data = rows;
});
console.log(data);
Normally, console need to return rows’ data but It returns ’empty’. How can I query rows from inside of function? How can I define a global variable?
The reason it is not working is because you
console.logis outside the asynchronous code block. Basically what is happening is this:datais set toempty;connectionissues a database request;console.logfires (dataisemptyat that point );datais set torows.So in order to get what you want simply put
console.logstatement inside an asynchronous block code: