return value from callback in node.js and mongoose return value from callback in node.js and mongoose mongoose mongoose

return value from callback in node.js and mongoose


You need a callback function since this is an async request:

function authenticate(accesskey, callback)  {    var auth = null;    userModel.findOne({'uid': accesskey}, function(err, user) {        console.log("TRY AUTHENTICATE");        if (err) {            console.error("Can't Find.!! Error");        }        //None Found        if (user === null) {            console.error("ACCESS ERROR : %s  Doesn't Exist", accesskey);            auth = false;        } else {            console.log(user);            auth = true;        }        callback(auth);    });}

And call this function like :

authenticate("key", function (authResult) {    //do whatever});


You can also try with promises.

function authenticate( accesskey )  {    var promise = someModel.findOne({'uid': accesskey}).exec();}routeHandler( req, reply ) {      authenticate(req.params.accesskey)      .then(function (auth) {          if(auth) {              //"primary code"          }           else {              //fallback          }      })      .catch(console.error);  }