0

i got a problem understanding async callbacks in node.js

this is my code :

//userRole is a session value

//capabilities is an array of strings

var check = 0;

capabilities.forEach((index)=> {

connection.query("SELECT * FROM doc_users_role WHERE name = ?

AND " + index     + "= 1 ",[userRole], function(err, rows){

if(rows.length == 1){

check++;}

});
})
console.log(check) //returns always 0;

How can i solve this problem in the async way? i need to update the variable check from within a callback (the connection.query one) that lives inside a foreach...thank you !

1
  • It looks like you are doing this asynchronously. The function you have passed as the last parameter is the callback. Commented Jan 20, 2017 at 15:49

1 Answer 1

1

This can be achieved using the async module as shown below. Because of nodejs non-blocking nature it will continue to execute without waiting for the loop to finish unless told otherwise. This is where callbacks come in. Each iteration of the forEachSeries loop will execute and then a callback is executed to tell the loop that it is ready for the next iteration. This way each iteration is done in turn. The console log is then placed in the final block, this is the piece of code that will be executed on completion of the forEachSeries and so ensures that the correct value for the variable is shown. I hope this answers your question.

        async.forEachSeries(capabilities, function (index, forEachCallback){ 

            connection.query("SELECT * FROM doc_users_role WHERE name = ?

            AND " + index     + "= 1 ",[userRole], function(err, rows){

                if(rows.length == 1){
                    check++;
                    forEachCallback();
                }
                else{
                    forEachCallback();
                }

            });

        }, function(err) {
            if ( err ){
                // catch error and do something
                return;
            }
            console.log( check );
        }); 
Sign up to request clarification or add additional context in comments.

2 Comments

this was great. now i have the last problem. "capabilities" is a method of the class "Roles" that i invoke when i get to some particular routes. The fact is that i need to return the whole capabilities method as true, so that when i'm making a get request for a route node firstly check if "roles.capabilities" is true, and if it is true, it sends some response... could you please help me? as i can't manage to return the whole function "true" when the var "check" is equal to capabilities.length... thank you so much
If i understand your problem correctly then you should replace the console.log( check ); with an if statement checking if check is equal to roles.capabilites.length and if so set roles.capabilities equal to true, simply like roles.capabilities = true, hope this helps solve your problem, if my answer above has solved your initial problem please mark it as correct, thankyou

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.