javascript - NodeJS MySQL How to get the result outside of the query function -


i can't seem figure out how result outside of nodejs mysql pool query. here sample code better explain mean.

var result = 'hello world!';  var mysql = require('mysql'); var pool = mysql.createpool({     connectionlimit : 100,     host            : process.env.db_host,     user            : process.env.db_username,     password        : process.env.db_password,     database        : process.env.db_database });  pool.query('select * user limit 10', function (err, rows) {     result = rows; });  res.send(result); 

the above return 'hello world!' instead of query.

if console.log(result) inside pool.query function returns rows query can't seem data outside of function. i've logged function , checked associated functions , think i'm missing basic.

you're sending results before query finishes (and callback called). sending results in callback fix problem:

pool.query('select * user limit 10', function (err, rows) {     result = rows;     res.send(result); }); 

as aaron pointed out, common problem. more thorough answer can found here.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -