When I try to execute SQL result console.log(row.plan);
or this.row.plan
; outside of function, I get error:
Cannot read properties of undefined (reading 'plan')
This is how I fetched result plan
:
var row;
var plan;
MySQL.query("SELECT * FROM users WHERE username = 'onyx'", (err, result) => {
if(err) { console.log(`[!] SQL Error: ${err}`); return; }
Object.keys(result).forEach(function(key) {
row = result[key]
console.log(row.plan)
});
});
The problem is that when I try to use console.log(row.plan);
outside of this Object.keys...
function, I get an error Cannot read properties of undefined (reading 'plan')
.
For example:
MySQL.query("SELECT
...
Object.keys(result).forEach(function(key) {
row = result[key]
});
console.log(row.plan) // HERE
});
console.log(row.plan) // OR HERE
I defined row
and plan
outside with var
. I am new to JavaScript so I am not sure what to use. I tried with let
and const
but both gives the same error.
I use node V16.15.1