Home > front end >  (node:14224) UnhandledPromiseRejectionWarning
(node:14224) UnhandledPromiseRejectionWarning

Time:11-30

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().

CodePudding user response:

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);
  });
});
  • Related