scottmont scottmont - 5 months ago 24
Node.js Question

NodeJs need to wrap function in a callback

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
};

Answer

Without more details, it's difficult to guess what this method is doing and what value do you expect to be returned.

I believe, however, you're trying to get the token with this method. So here's what you might be looking for:

exports.gtoken = function(req, sfinfo, callback) {
    req.app.db.models.Account.findById(req.user.roles.account.id, 'isVerified extraemail search memberid name').exec(function(err, account) {
        if (err) {
            callback(err, null);
        } else {
            var outcome = {};
            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');
            callback(null, token);
        }
    });
};

And here's how you could use the method:

gtoken(req, sfinfo, function(err, token) {
    if (err) {
        console.error(err + 'issue');
    } else {
        console.log(token);
    }
});
Comments