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 !