I'm having trouble getting a return value from an Asynchronous function in JavaScript. I have tried a few different approaches based off of other posts found here - How to properly return a result from mysql with Node? and here Store mysql query rows in variable for later use. My current code is as follows .
function get_info(callback) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'xxxxxxx',
user: 'bob',
password: 'bob',
database: 'xxxxxx'
});
connection.connect(function (err) {
if (err) throw err;
connection.query("SELECT Name FROM webform", function (err, result) {
if (err) throw err;
return callback(result);
});
connection.end();
});
}
var newli = get_info(function(result){
stuff_i_want = result;
return stuff_i_want;
});
console.log(newli);
I'm expecting the console.log(newli)
to print the value of stuff_i_want
which I'm returning from the get_info()
function but I instead get undefined. I'm trying to get the value of result
from the connection.query
stored in a variable that I can use later.