I'm trying to store a query result from mysql using node into a variable and log that variable to the console. Because of the nested function within the method, I am not getting any returned results that are usable. I've tried several things to make this work but am looking for the most simple solution. Here's my code:
const mysql = require('mysql');
var billsSelectAllSQL = 'SELECT * FROM bills';
/* Start connection to MySQL */
var mysqlConnection = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "budget"
});
mysqlConnection.connect(function(err) {
if(err) {
throw err;
}
console.log("MySQL database connected!");
});
var mysqlQuery = function(query) {
var queryResult;
mysqlConnection.query(query, function(err, result, fields) {
if(err) {
throw err;
}
queryResult = result;
});
return queryResult;
};
var bills = mysqlQuery(billsSelectAllSQL);
console.log(bills);