I tried the following code.
function authenticate( accesskey ) {
var res = someModel.findOne( {'uid': accesskey}, function ( err , user) {
if(err){
console.error("Can't Find.!! Error");
}
if(user===null){
return false;
}
else{
console.log(user);
return true;
}
});
console.log(res);
return res;
}
but res
here returns a mongoose data type.
I wish to call the authentication function like this -
if(authenticate(req.params.accesskey)){
//do something
}
else{
//do something else
}
UPDATE after implementing SOLUTION from Mustafa Genç
After getting comfortable with callbacks I ended up with the following code.
function authenticate( req, result, accesskey, callback ) {
var auth = null;
someModel.findOne( {'uid': accesskey}, function ( err , user) {
console.log("try authenticate");
if(err){
console.error(err);
}
if(user===null)
auth = false;
else
auth = true;
callback(auth);
});
}
And I use it like this -
routeHandler( req, reply ) {
authenticate( req, reply, req.params.accesskey , function (auth) {
if(auth) {
//"primary code"
}
else {
//fallback
}
});
}