mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.96k stars 213 forks source link

promise created in a handler but not returned #166

Closed terryds closed 5 years ago

terryds commented 6 years ago

This is the error:

Warning: a promise was created in a handler at D:\Playgrounds\express-mongoose-es6-rest-api\config\passport.js:15:18 but was not returned from it

This is my passport.js:

var JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;

// load up the user model
var User = require('../server/user/user.model');
var config = require('./config'); // get db config file

module.exports = function(passport) {
  var opts = {};
  opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
  opts.secretOrKey = config.jwtSecret;
  passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
    User.findById(jwt_payload._id)
      .then(user => {
          return done(null, user);
      })
      .catch(err => {
          return done(err);
      })
  }));
};
But, I suspect the main problem is my route controller (store.controller.js):

const Store = require('./store.model');

exports.add = async function(req, res) {

  const store = new Store({
    name: req.body.name,
    description: req.body.description,
    mobileNumber: req.body.mobileNumber,
    is_online: req.body.is_online,
    open_schedule: req.body.open_schedule,
    address: req.body.address,
    creator: req.user._id
  });

  try {
    return store.save()

   // I already have tried store.save().then(savedStore => return res.json(savedStore)), but still no help.
  }
  catch(e) {
    return res.status(403).send({success: false, msg: 'Unauthorized.'})
  }

}

If I don't use save(), and just return a message directly (like return res.json({msg: "Hi"}) ), it goes without error. Please help me

joshdenz commented 5 years ago

I think this can probably be closed:

"It turns out I forget to call next() on my model pre-save. I spend 'bout two days figuring this out. So, if anyone reading this has the same problem, make sure you call next()"

https://stackoverflow.com/questions/51773343/mongoose-promise-with-bluebird-error-with-passport-js

mikenicholson commented 5 years ago

closing.