javascriptexpressjwtpassport-jwt

(node:14224) UnhandledPromiseRejectionWarning


I am using below for JWT:

let jwtoptions = {};
jwtoptions.jwtFromRequest = ExtractJWT.fromAuthHeaderAsBearerToken();
jwtoptions.secretOrKey = DB.secret;
let Strategy = new JWTStrategy(jwtoptions,(jwt_payload,done)=>{
  User.findById(jwt_payload.id)
  .then(User =>{
    return done(null, User);
  })
  .catch(err =>{
    return done(err,false);
  })
})
passport.use(Strategy);

But its showing me below error:

(node:14224) UnhandledPromiseRejectionWarning: TypeError: done is not a function
(node:13568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

Solution

  • Haven't used passport-jwt but from reading the docs it seems you need to use a callback instead of a promise

    let Strategy = new JWTStrategy(jwtoptions,(jwt_payload,done)=>{
      User.findById(jwt_payload.id, (err, user) => {
       if (err) return done(err, false);
       return done(null, user);
      });
    });