1

I want to verify incoming token and I try to find user by Id:

module.exports = function (req) {
    var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
    db.users.findById(decodeToken.id).then(function (foundUser) {
        //It's example checking for self-learning
        if (foundUser.username == req.cookies.username) {
            return foundUser;
        }
        //Or more logic for token authentication
    }), function (error) {
        return error;
    }

But I get "return false". I view foundUser variable for debuggindg, and it has message

'Reference Error: foundUser is nor defined'

In console I can see query:

Executing (default): SELECT "id", "username", "email", "password", "createdAt", "updatedAt" FROM "users" AS "users" WHERE "users"."id" = 2;

And I have the user with id=2 in a db. Why it doesn't work?


Addition:

I tried MWY's modified example:

module.exports = function (req) {
    var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
    findUserById().then(function(foundUser) {
        //It's example checking for self-learning
        if (foundUser.username == req.cookies.username) {
            return foundUser;
        }
        //Or more logic for token authentication
    }), function (error) {
        return error;
    }

    function findUserById() {
        return db.users.findById(decodeToken.id).then(function (foundUser) {
            return foundUser;
        }), function (error) {
            return error;
        }
    }
}

And get error:

TypeError: findUserById(...).then is not a function

5
  • There must be some code that you haven't posted yet. I get a feeling you are misunderstanding how asynchronous programming works, judging by your return foundUser line. If you add console.log(foundUser) before the return, what do you get?
    – Sven
    Apr 2, 2017 at 20:55
  • I'm not really understand how to write asynchronous code. I try to change my question and add the code.
    – Sergei R
    Apr 3, 2017 at 19:59
  • There are lots of explanations here on Stackoverflow about asynchronous programming in JavaScript. It is probably the most asked question here, so search for it and you should get many results. In your case, finding a user in the database takes time, so you pass a function (in the .then() method) that will be called when there is a result or error (somewhere in the future).
    – Sven
    Apr 3, 2017 at 22:01
  • Hi, guys, you should not write like that, You did not understand what I meant
    – MWY
    Apr 4, 2017 at 1:19
  • @Svenskunganka This principle I understand.
    – Sergei R
    Apr 4, 2017 at 3:44

1 Answer 1

3

Be sure to remember asynchronously

Because asynchronously! you will get false first, then get the result!

So you can write like this, In ty.js file

      module.exports = function (req) {
      var decodeToken = jwt.decode(req.cookies.token,   JwtOptions.secretOrKey);
      return db.users.findById(decodeToken.id).then(function (foundUser) {
    //It's example checking for self-learning
    if (foundUser.username == req.cookies.username) {
        return foundUser;
    }
    //Or more logic for token authentication
    }).catch(function (err) {
    return err;
     })
   };

In tu.js file:

   var user = require('./ty');

   user().then(function (result) {
   //search findById result
   console.log(result)

   });
2
  • I still don't understand arrow syntax. The ".catch(() => { and .then((value) => {" called "expression expected".
    – Sergei R
    Apr 3, 2017 at 19:53
  • this method return db.users.findById() leads to error (stackoverflow.com/questions/43300185/…)
    – Sergei R
    Apr 11, 2017 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.