so I am trying to create a function that checks if the name of an author is already saved in my database. I am a beginner with Node.js, so I am not used to working with modules, I have always used OOP.
exports.checkAuthor = function(authorName) {
var inDatabase;
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = `SELECT COUNT(*) AS 'countResult' FROM Author WHERE Name LIKE '${authorName}'`;
conn.query(sql, function (err, result) {
if (err) throw err;
inDatabase = (Object.values(result[0])[0] == 1)? true:false;
return inDatabase;
});
conn.end();
});
}
It should return either true or false depending on if the value stored is 1 or 0. And I am trying to get the return value like this:
console.log(db.checkAuthor('Drake'));
But it keeps returning undefined... I checked and the value of
inDatabase = (Object.values(result[0])[0] == 1)? true:false;
is in fact always true or false. Thanks for any help!