gpt4 book ai didi

javascript - 如何使用 Bluebird.js nodeify 将第三个参数传递给回调

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:52 24 4
gpt4 key购买 nike

With a little help我已经到达以下代码来 promise a passport.js login strategy.

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var Promise = require('bluebird');
var bcrypt = require('bcrypt');
var db = require('./db').db; //users are stored in mongo

//I'm using bluebird.js for promises
var users = Promise.promisifyAll(db.users);
var compare = Promise.promisify(bcrypt.compare);


// This strategy is used by passport to handle logins
module.exports.localStrategy = new LocalStrategy(function(username, password, done) {
users.findOneAsync({username: username}).bind({})
.then(function(user) {
if (!user) {
throw new NoMatchedUserError('Incorrect username.');
//should be equivalent to:
// return done(null, false, {message:'something'});
}
this.user = user;
return compare(password, user.password);
})
.then(function(isMatch) {
if (isMatch) {
return this.user;
//is equivalent to:
// return done(null, this.user);
}
else {
throw { message: 'Incorrect password.' };
//should be equivalent to:
// return done(null, false, {message:'something else'};
}
})
.nodeify(done);
});

调用 nodeify(done)我可以处理密码匹配的路径,但我不知道如何将可选的第三个参数传递出去,以便 passport.js 可以使用它。

是否可以处理两个失败(不是错误)路径?


更新:

正如评论中所问,我在 Github 上创建了一个问题,并且(非常迅速地)在 Bluebird v2.0 中添加了此功能

https://github.com/petkaantonov/bluebird/issues/219

最佳答案

使用:

.nodeify(done, {spread: true});

这允许将多个参数传递给“完成”回调。

更多信息:

Bluebird nodeify documentation

关于javascript - 如何使用 Bluebird.js nodeify 将第三个参数传递给回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com