I have a function isValidCode(code) which evaluates to true and returns a boolean(I've checked the type using typeof()).
I use this function as the conditional of an if statement:
if(isValidCode(code)){
    console.log('Code is valid!');
}else{
    console.log('Code is not valid!');
}
This is not working for some reason, and the else statement is executed even though the function evaluates to true. Why is this? I'm using node.js.
isValid function:
exports.isValidCode = pool.pooled(function(client, Code){
  var valid;
  client.query('SELECT EXISTS(SELECT * FROM codes where code = $1)', [Code], function(err,result){
    if (err) console.log('isValidCode error: ' + err);
    console.log('isValidCode?: ' + result.rows[0].exists);
    valid=result.rows[0].exists;
    console.log(typeof(result.rows[0].exists));  
  });
  return valid;
});
 
     
    