I'm trying to make a function that returns true if it detects a row containing a value in a specific column in nodejs.
I've tried to use the result variable from query() without any success :
let rowexists = (mystring) => {
let exists = false;
let sql = "SELECT EXISTS( SELECT 1 FROM mytable WHERE `mycolumn` = '" + mystring + "')";
connection.query(sql, function(error, result, field){
console.log((result[sql]));
console.log(exists);
exists = (result[sql]);
});
return exists;
}
console.log(rowexists("myvalue"));
Event if there is a row with the value "myvalue" (there is), rowexists() always returns false.
IMPORTANT EDIT:
My problem isn't really the fact that this is async, it's the fact that both
console.log((result[sql]));
and
console.log(exists);
return undefined.