I'm using mysql with nodejs for the first time and I wish to enter the result from an sql query in my webpage through an asynchronous function. I am able to get the correct result but unable to store it within a variable and then use it as an input value.
I've tried using this but this doesn't provide a viable solution. I tried returning the value as well. Also will it be advisable to process the query outside the async function or inside it?
(async function Ahmedabad() {
let i = 'select RegNumber from Details ORDER BY RegNumber DESC LIMIT 1;';
await con.query(i, [true], (error, results, fields) => {
if (error) {
return console.error(error.message);
}
let x = results[0].RegNumber;
console.log(x); // <------- Shows correct value
});
console.log(x); // <------- Says x is not defined
})()
I want to store the value into a variable and then use it further.