I am working to get data off my SQL database and right now I have a users table. The two colums im working with are 'AccountKey' which is just a string and 'Friends' which is a json table written as a string.
I have a function like so to retreive an array of a user's friends:
var Proceed = false;
conn.query(`SELECT Friends FROM users where AccountKey = "${data}"`, function(err, result){
if (result.length > 0){
var Friends = JSON.parse(result[0].Friends)
for(var i in Friends){
if (i == Friends.length - 1){
Proceed = true;
}
conn.query(`SELECT Name FROM users where AccountKey = "${Friends[i]}"`, function(err, result){
if(result.length > 0){
console.log(Proceed)
}
});
}
}
});
Proceed is a variable im going to use to explain my problem:
Long story short, in the console.log
line, proceed is logged as 'true' result.length times, when it's only meant to be logged as true once.
Variables work very weirdly within the scope and I'm not sure if i am missing something. Any help is appreciated