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

Sergei R
  • 701
  • 4
  • 10
  • 24
  • 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 02 '17 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 03 '17 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 03 '17 at 22:01
  • Hi, guys, you should not write like that, You did not understand what I meant – MWY Apr 04 '17 at 01:19
  • @Svenskunganka This principle I understand. – Sergei R Apr 04 '17 at 03:44

1 Answers1

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)

   });
MWY
  • 1,071
  • 7
  • 15
  • I still don't understand arrow syntax. The ".catch(() => { and .then((value) => {" called "expression expected". – Sergei R Apr 03 '17 at 19:53
  • this method **return** db.users.findById() leads to error (http://stackoverflow.com/questions/43300185/error-with-query-sending-unhandled-rejection-sequelizeconnectionerror-read-econ) – Sergei R Apr 11 '17 at 17:24