0

This is my following code I'm unable to get the 'check'variable value can you find the issue where I made mistake :-)

checkUsr = function() {
  var q1 = "select * from Mytable where id='123';";

  var check= null;

  con.query(q1, function(error, rows, fields) {
    if(error) {     
        console.info('11111111');
        check={
            "response":"error", 
            "responseString": JSON.stringify(error)
        };
        // return check;        
    } else {
        console.info('222222222');
        if(rows.length>0){
            check = {
                "response":"error", 
                "responseString": "already sent"
            };      
            // return check;
        }else{
        console.info('33333333');
            check= {
                "response":"success",
                "responseString":"go ahead..!!"
            };
            // return check;
        }
    }
    return check;
  });
  return check;
}
checkUsr(); 

expected value the value of check object inside the if/else block

But output

null

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lokesh G
  • 831
  • 11
  • 19
  • 1
    The final `return check;` statement is called before the query callback is called, and therefore does not have a value yet. It's not a scope problem in your case – Loupax Sep 02 '14 at 10:13
  • thanks @Loupax ,can you help me to fix this issue :-) – Lokesh G Sep 02 '14 at 10:18
  • In order to help you I need to know how you plan to use the check variable. At this moment all you know is that you have a variable named `check` and that *sometime* will have it's value changed. The safest bet could be to run the function that uses the check variable at the end of the callback, but I can't tell if this is what you need in this case – Loupax Sep 02 '14 at 10:24
  • i need return value of checkUsr() from callback function :-) – Lokesh G Sep 02 '14 at 10:55

0 Answers0