0

it doesnt change to false!! please help i need to change the value of Password to false

//password check
{

 connection.connect(function(){
    connection.query('select * from rencho2.user',function(err,results){ 
        if(results[0].passwd==p){
            console.log("correct");
        else { 
               global.verify="false";
           console.log("Incorrect. "); //here its false                             
        }                                 
         //here its false
   }); 
                //here it becomes true -- why??

    send = { 
        "UserName": body.UserName,
        "Password": global.verify
    }
    body1=JSON.stringify(send);
});

}); 
user229044
  • 232,980
  • 40
  • 330
  • 338
mimzah
  • 31
  • 3
  • Not the bees! My eyes! Not the bees!! – dwerner Oct 29 '13 at 17:16
  • Please provide your entire implementation. Where is this verify variable being declared? – Gautam Bhutani Oct 29 '13 at 17:17
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – user229044 Oct 29 '13 at 17:18
  • Your anonymous function is probably getting called after you create the send object – Matthew Mcveigh Oct 29 '13 at 17:21
  • This is the 3rd such question I have seen where the developer is completely oblivious to this characteristic of async code the last few days. – dwerner Oct 29 '13 at 17:24
  • 1
    @dwerner This class of question gets asked several times per day. Vote to close a duplicate of http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call and move on. – user229044 Oct 29 '13 at 17:25

2 Answers2

2

You're dealing with asynchronous and synchronous code. connection.query executes after the outer code is done. It is only at that point, global.verify is false. Prior to that global.verify is true because the callback to connection.query has not executed yet. You should do what you need from within the callback:

connection.query('select * from rencho2.user',function(err,results){ 
    if(results[0].passwd==p) {
        console.log("correct");
    } else { 
        global.verify="false";
        console.log("Incorrect. "); //here its false                             
    }                                 

    send = { 
        "UserName": body.UserName,
        "Password": global.verify
    };

    body1 = JSON.stringify(send);

    //do what you need with body1  
});
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

The code seems strange, and with no context it's a hard question to answer, but generally asynchronous behaviour is a good thing, but if for some strange reason you really need it to be synchronous you can always fake it :

connection.connect(function(){
    var done = false;

    connection.query('select * from rencho2.user', function(err,results) { 
        if(results[0].passwd == p){
            console.log("correct");
        } else { 
            global.verify = false;
            console.log("Incorrect. ");
        }
        done = true;
    }); 

    while (done == false) {};

    send = { 
        "UserName": body.UserName,
        "Password": global.verify
    }
    body1 = JSON.stringify(send);
});

note: this is generally not a good idea, but will work as a last resort !

adeneo
  • 312,895
  • 29
  • 395
  • 388