每次我访问需要身份验证的路线时,我都会在控制台中收到警告消息。
(node:940) 警告:在 xxxxxx\app\config\passport.js:15:19 处的处理程序中创建了一个 Promise,但未从中返回,请参阅 http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it 在 .fetch (xxxxxx\node_modules\bluebird\js\release\method.js:13:13)
我已经像这样配置了 Passport :
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const secret = process.env.SECRET;
var opts = {}
function passportConfig(db, passport) {
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
opts.secretOrKey = secret;
passport.use(new JwtStrategy(opts, payloadCallback.bind(null, db)));
}
function payloadCallback(db, payload, done) {
new db.User({id: payload}).fetch()
.then(response => response.toJSON())
.then(user => done(null, user))
.catch(err => console.log(err));
}
module.exports = passportConfig;
如有任何帮助,我们将不胜感激。
最佳答案
我通过用 .asCallback(done) 替换第二个 then 和 catch 来修复此警告。
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const secret = process.env.SECRET;
var opts = {}
function passportConfig(db, passport) {
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
opts.secretOrKey = secret;
passport.use(new JwtStrategy(opts, payloadCallback.bind(null, db)));
}
function payloadCallback(db, payload, done) {
new db.User({id: payload}).fetch()
.then(response => response.toJSON())
.asCallback(done);
}
module.exports = passportConfig;
关于node.js - Promise 已创建但未返回。膝关节/书架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40475313/