node.js - Promise 已创建但未返回。膝关节/书架

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

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

(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/

相关文章:

javascript - 从 node.js 检索时 Mysql 日期不同

node.js - 将更新的更改 pull 入已部署的 Docker 容器的最佳方法是什么?

javascript - 如何在 node.js 中弹出 CD 驱动器?

JavaScript promise 初学者

javascript - 如何将 Array.prototype.map() 与 then() 一起用于 connection.end()

node.js - NodeJS - MongoDB 触发器

javascript - 为什么 Promise.all 不异步运行 2 个函数?

javascript - Express.js - foreach 数据数组并将它们存储在数据库中

node.js - 在 KNEX/BookshelfJS 中定义 'pool' 的语法

mysql - 尝试使用书架将经过身份验证的用户传递给nodejs中的数据库 Controller