Promise已创建但未返回。 Knex / Bookshelf

时间:2016-11-07 21:51:59

标签: node.js promise bookshelf.js knex.js passport.js

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

(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     at .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;

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

我通过替换第二个然后使用.asCallback(已完成)来修复此警告。

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;