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);
}
});
err,rows, andfieldsyou can check ifrows[0]is undefined.