I have written some code where I have a variable that changes if I enter an if statement inside the function of an sql query. However the function does not change globally only locally. The code is the following:
app.post("/energy/api/Admin/users", function(req,res)
        var correct=0
        sql.query(`SELECT apikey,privileges FROM users WHERE apikey=?`,[req.user.apikey],(err,res1) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
            else if (res1.length && res1[0].privileges=="superuser"){
                console.log("Apikey correct");
                correct=1;
            }       
            else  correct=0;
        });
        console.log(correct);
        if (correct==1){
                 ....
                }
Note: The if (correct==1) never happens even if correct is set to 1 inside the query function. I think this is not supposed to happen with variables in javascript. Why does this happen?
 
    