1.完整报错信息:
MongooseError: Model.findOne() no longer accepts a callback
at Function.findOne(C:\Users\DELL\Desktop\node-app\node_modules\mongoose\lib\model.js:2351:11)
at JwtStrategy._verify (C:\Users\DELL\Desktop\node-app\config\passport.js:12:14)
at C:\Users\DELL\Desktop\node-app\node_modules\passport-jwt\lib\strategy.js:123:34
at C:\Users\DELL\Desktop\node-app\node_modules\jsonwebtoken\verify.js:261:12
at getSecret (C:\Users\DELL\Desktop\node-app\node_modules\jsonwebtoken\verify.js:97:14)
at module.exports [as verify] (C:\Users\DELL\Desktop\node-app\node_modules\jsonwebtoken\verify.js:101:10)
at module.exports [as JwtVerifier] (C:\Users\DELL\Desktop\node-app\node_modules\passport-jwt\lib\verify_jwt.js:4:16)
at C:\Users\DELL\Desktop\node-app\node_modules\passport-jwt\lib\strategy.js:104:25
at JwtStrategy._secretOrKeyProvider (C:\Users\DELL\Desktop\node-app\node_modules\passport-jwt\lib\strategy.js:40:13)
at JwtStrategy.authenticate (C:\Users\DELL\Desktop\node-app\node_modules\passport-jwt\lib\strategy.js:99:10)
尝试解决方法:从第三行开始的一系列地址信息,点击ctrl+地址跳转,找到项目中自己创建的JS页面(不要去修改node_modules中的页面)里的findOne函数,把回调函数改成promise写法,加上.then()函数
//此种写法会报错
module.exports = passport => {
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
User.findOne({ id: jwt_payload.sub }, function (err, user) {
console.log(jwt_payload);
});
}));
}
//需修改为promise形式,例如:
module.exports = (passport) => {
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
User.findOne({ id: jwt_payload.sub }).then((err, user) => {
console.log(jwt_payload);
});
}));
}
修改之后可以收到前端请求信息。