0

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

Onyx
  • 15
  • 6
  • Why closed immediately? I still didn't get the answer i am looking for... – Onyx Jun 08 '22 at 22:05
  • Waited 1.5h to post new question with better explanation and I noted that I tried what i've been told to... – Onyx Jun 08 '22 at 22:06

0 Answers0