I am still fairly new to NodeJS. I realize I need to put the following code in a callback so I can wait for the database call to finish, but I can't see how to get it to work.
I need to return my result to the parent function where marked at the bottom of this code:
exports.gtoken = function(req, sfinfo) {
var outcome = {};
req.app.db.models.Account.findById(req.user.roles.account.id, 'isVerified extraemail search memberid name').exec(function(err, account) {
if (err) {
return console.error(err + 'issue');
}
outcome.account = account;
var memberid = JSON.stringify(outcome.account);
var g = (new Date().getTime())/1000;
var n = (g + 86400);
var memberid = outcome.account.memberid;
var secondaryemails = outcome.account.extraemail;
var isVerified = outcome.account.isVerified;
var IDName = outcome.account.name;
var payload = {
"email": req.user.email ,
"exp": n,
"iss": req.app.locals.issid ,
"userid": req.user.id ,
"signedin": 'true' ,
"memberid": memberid ,
"secondaryemails": secondaryemails ,
"isVerified": isVerified ,
"IDName": IDName ,
"sfinfo": sfinfo,
};
var token = jwt.encode( payload, req.app.config.cryptoKey, 'HS512' );
console.log(token);
return token;
});
// I need to return my result here to the parent function
};