0

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

Adaptabil
  • 15
  • 6
  • `conn.query` is asynchronous; the for loop will start all those queries, then the last iteration will set `Proceed = true` and some time *after that*, all those queries will finish. If you want to write synchronous code you need to use a promise based mysql api and `await` the result of each query. –  Jan 17 '21 at 22:18
  • In that case why wouldn't i be able to make an array outside of the scope and push every element returned by the query into the array? (I've tried that before too, the array always ends up being empty) Can you give me any links that i can read up more on these promies? – Adaptabil Jan 17 '21 at 22:20
  • That will work in theory, but a) the order in which the queries will finish is arbitrary b) due to conn.query being asynchronous, you logged the array when it was still empty, long before the first query finished. Asynchronous code is the #1 beginner's pitfall currently, by far. There is tons of reference, primarily this: https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron –  Jan 17 '21 at 22:22
  • I'll give these resources a shot and hope for the best. Thank you for the help – Adaptabil Jan 17 '21 at 22:25
  • If you use the mysql2 package instead, Promises are ready to go: https://www.npmjs.com/package/mysql2#using-promise-wrapper –  Jan 17 '21 at 22:27
  • I've read up on promises and asynchronity etc. While it took a while to wrap my head around it, I managed to get it working. Thank you so much for the help – Adaptabil Jan 18 '21 at 13:39
  • You're welcome, glad you got it sorted ;) –  Jan 18 '21 at 13:40

0 Answers0