I am trying to execute SQL results outside it's function. This is my code:
var rows;
var fields;
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'root'
});
connection.connect(
(err) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log('logged in!');
}
);
async function query() {
[rows, fields] = await connection.execute("SELECT plan FROM users WHERE username = 'onyx'");
console.log(rows);
}
console.log(rows);
query();
First console.log(rows); gives correct output but once I put it outside the function, I get error undefined.
I know there are questions about this but I need better understanding and explanation about this...
I am using sync now as told but still same problem.
await query();and then after that log the rows, the rows should be defined nowawait query();when im calling the function or within a function?