I've been trying to use async/await with MySQL in node but it returns an undefined value each time. Is there a reason why? Please find my code below.
const mysql = require('promise-mysql');
var connection;
const dbConfig = {
host: "hostname",
database: "dbname",
user: "username",
password: "passwords"
};
async function getResult(){
await mysql.createConnection(dbConfig).then(function(conn){
connection = conn;
var result = connection.query('select height from users where pin=1100');
return result;
}).then(function(rows){
console.log(JSON.parse(JSON.stringify(rows[0].height)));
connection.end();
return rows[0].height;
}).catch(function(error){
if (connection && connection.end) connection.end();
//logs out the error
console.log(error);
});
}
async function queryDb(){
try{
var height = await getResult();
console.log(height);
if(height){
console.log(height)
}
}catch(err){
console.log(err);
console.log('Could not process request due to an error');
return;
}
}
queryDb();
I expect the height to be returned in queryDb, however, the value is only shown in the getResult function and not returned to be used in the queryDb function.
I know the code may not be perfect as I'm new to node and I've been trying to find alternative ways to do this but