dimanche 29 mars 2015

Mongoose set local variable from query

I have a function which basically queries the MongoDB for a credential (simplified):



var query = Credential.findOne({token: token});

return query.exec(function (err, credential) {
if (err) return null;
return credential;
});


I need this credential for another query to get associated roles:



router.get('/api/roles', function (req, res) {
var token = req.headers.authorization || '';
var cred;

if (token === '') {
return res.sendStatus(401);
}
getCredential(token).then(function (credential) {
cred = credential;
});

getRoles(cred).then(function(roles) {
return res.json(roles);
});
});


Unfortunately, I can't seem to set the local variable cred from within the resolved promise and thus it is undefined. Any idea how to do this?


Aucun commentaire:

Enregistrer un commentaire