Promise 已创建但未返回。 Knex / 书架

Promise was created but not returned from. Knex / Bookshelf

每次我访问需要身份验证的路由时,我的控制台都会收到一条警告消息。

(node:940) 警告:在 xxxxxx\app\config\passport.js:15:19 处的处理程序中创建了一个承诺,但未从中返回,请参阅 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)

我是这样配置护照的:

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;