-1

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
};
Tim Malone
  • 3,364
  • 5
  • 37
  • 50
scottmont
  • 39
  • 6
  • 1
    How are we supposed to know what "does not work" specifically? – zerkms Jun 10 '16 at 04:21
  • sorry just I get the token in console after the return. – scottmont Jun 10 '16 at 04:31
  • Ok, so? You've got it in the console? Having it in console is the problem you're talking about? – zerkms Jun 10 '16 at 04:32
  • 1
    Very poorly written question. You should edit the question to be much more clear about what the problem is and what you want help with. It looks like @JasonWihardja may have guessed appropriately what you are asking about, but good questions should not require guessing. They should explicitly state exactly what your problem is and exactly what you want help with. – jfriend00 Jun 10 '16 at 04:33
  • You will probably also benefit from reading this [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323). – jfriend00 Jun 10 '16 at 04:34

1 Answers1

2

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);
    }
});
  • One word: promises – zerkms Jun 10 '16 at 04:32
  • A guy at work keeps telling my about promises. I will google that up now. – scottmont Jun 10 '16 at 04:34
  • Thanks so much Jason I am trying it out. – scottmont Jun 10 '16 at 04:34
  • Promise is another way to handle asynchronous operations. But since you're asking for callbacks, so there you have it –  Jun 10 '16 at 04:36
  • @JasonWihardja they are asking about callbacks because they don't know about promises. XY-problem. – zerkms Jun 10 '16 at 04:37
  • I tried using callback myself and i got this same error as i get now with your code. `TypeError: callback is not a function` – scottmont Jun 10 '16 at 04:39
  • @scottmont so we had to guess about your initial problem, now we need to guess about your current code. Do you honestly think it is a most efficient way of solving problems? – zerkms Jun 10 '16 at 04:40
  • sorry there isn't much to it I thought I posted everything relevant – scottmont Jun 10 '16 at 04:43
  • You have, and you've got an answer with proper code that would work. If it does not - you need somehow to share what you actually have at the moment. The scientific guess would be: you're not invoking the `gtoken` function correctly, but not sure why to guess instead of dealing with facts (that you're obliged to provide). – zerkms Jun 10 '16 at 04:52
  • I apologize for not being more concise. As I said I am still learning. I copied and pasted that exact code and call it using I get `callback is not a function` – scottmont Jun 10 '16 at 04:56
  • var gtoken = require('../../util/tokengen/token.js'); var token = gtoken.gtoken(req, sfinfo, function(err, token) { if (err) { console.error(err + 'issue'); } else { console.log(token); } }); – scottmont Jun 10 '16 at 05:00
  • The return values are passed in to the callback function. You're not going to get what you want from assigning the function to a variable –  Jun 10 '16 at 05:14
  • @JasonWihardja , Thank you so much. I really appreciate you reading my mind and giving me the correct answer. Works great! – scottmont Jun 10 '16 at 05:16