0

I am trying to put the result of a mysql query into a variable so I can check if it is undefined or not and perform tasks based on that.

The problem is that the query does not seem to be working for some reason. Here is my code:

exports.check = function(zmail, password){

                var check = client.query('SELECT id FROM passwords WHERE
                                          email="'+zmail+'"');

                console.log(check);
                /*
                if(typeof check == 'undefined'){
                console.log("wrong email");
                }else{
                        var savedemail = check[0].id;
                        console.log(savedemail, zmail);
                }
                */

}

I know the query works though because it works as a function...I figure I must be putting it into the variable in the wrong way. Does anyone know how to do it correctly?

Update...I tried that answer but it did not work. When the id is undefined, the server crashes! Here's the code that doesn't work:

 var check = client.query('SELECT id FROM passwords WHERE email="'+zmail+'"', function(err,data){

                 if(data == null){
                console.log("wrong email");
                }else{
                        var savedemail = data[0].id;
                        console.log(savedemail, zmail);
                }
                });
3
  • 1
    If you are using the node.js mysql module, you need to specify a callback. your returned set will be available within that callback. github.com/felixge/node-mysql Commented Aug 8, 2013 at 18:10
  • I tried doing that at first but the problem is, if the function is undefined, the server crashes!...basically, the server says it can't take the id of something that's undefined and stops working. I need it to continue so that I can check if the variable is undefined or not and do something with it if not. Commented Aug 8, 2013 at 18:11
  • but you are not using a callback in the code above. Within your callback, with the arguments err, rows, and fields you can check if rows[0] is undefined. Commented Aug 8, 2013 at 18:20

1 Answer 1

1

Perhaps you need to use a callback like this:

exports.check = function(zmail, password){
  var check = client.query('SELECT id FROM passwords WHERE email="'+zmail+'"', function(err, data){
    console.log(data);
  });
}
Sign up to request clarification or add additional context in comments.

Comments

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.