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.log
is outside the asynchronous code block. Basically what is happening is this:
data
is set to empty
; connection
issues a database request; console.log
fires ( data
is empty
at that point ); data
is set to rows
. So in order to get what you want simply put console.log
statement inside an asynchronous block code:
var data = 'empty';
connection.query('SELECT * FROM deneme',function(err, rows, fields){
if (err) throw err;
data = rows;
console.log(data);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.