这是错误:
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
这是我的password.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);
})
}));
};
但是,我怀疑主要问题是我的路由控制器(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.'})
}
}
如果我不使用save(),而直接返回一条消息(例如return res.json({msg: "Hi"})
),那么它不会出错。
请帮助我
答案 0 :(得分:0)
尝试在您的护照代码中的用户之前添加return
。我认为错误是因为您没有返回外部承诺。
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
return User.findById(jwt_payload._id) //here
.then(user => {
return done(null, user);
})
.catch(err => {
return done(err);
})
}));
答案 1 :(得分:0)
事实证明,我忘记了在模型的保存前调用next()。
我花了大约两天时间来解决这个问题。
因此,如果有人阅读此书也遇到相同的问题,请确保您调用next()