I have a function that checks if a user is admin or not. If it finds the user id in the database table, it needs to return true
, otherwise it needs to return false
, but I'm getting undefined
. Can you help me?
function isAdmin(useruid) {
db.query("SELECT * FROM admins", function(err,rows,field) {
if(!err) {
rows.forEach(function(element) {
if (useruid === element.uid) {
console.log("[is admin] Found admins!")
return true;
} else {
console.log("[is admin] User is not admin!")
return false;
}
});
} else {
console.log(err);
return false;
}
})
}
I added ;
on the missing lines. I'm receiving output of the console.logs, but the function is still returning undefined
instead of false
or true
.
It looks like:
function isAdmin(useruid) {
db.query("SELECT * FROM admins", function(err,rows,field) {
if(!err) {
rows.forEach(function(element) {
if (useruid === element.uid) {
console.log("[is admin] Found admins!");
return true;
} else {
console.log("[is admin] User is not admin!");
return false;
}
});
} else {
console.log(err);
return false;
}
})
}