2014-01-21 49 views
6

我是Promises新手,并不知道如何解决此问题: 我正在做一个身份验证系统,我的第一个电话是检查数据库上的电子邮件。如果用户存在,然后检查密码对加密密码...我使用这个lib bcrypt:https://npmjs.org/package/bcrypt这是不承诺兼容,所以我使用“promisify”为以下签名:比较(密码,crypted_pa​​ssword,回电话)。蓝鸟promisify多个论据

所以这是我的代码:

var compare = Promise.promisify(bcrypt.compare); 

User.findByEmail(email) 
    .then(compare()) <--- here is the problem 

这是我findByEmail方法:

User.prototype.findByEmail = function(email) { 
var resolver = Promise.pending(); 

knex('users') 
    .where({'email': email}) 
    .select() 
    .then(function(user) { 
     if (_.isEmpty(user)) { resolver.reject('User not found'); } 
     resolver.fulfill(user); 
    }); 


return resolver.promise; 

}

如何将多个值分配给在这种情况下, “比较” 的方法?我错过了诺言的重点吗?

+0

究竟是如何做的是'user'变量是什么样子? – Bergi

+0

这是一个空数组,如果没有用户发现,或哈希数组 – rizidoro

+0

所以'user [0]'是'crypted_pa​​ssword'参数?你从哪里获得代码中的'password'? – Bergi

回答

5
.then(compare()) <--- here is the problem 

then method确实希望它返回另一个承诺[或普通值],所以你需要通过compare没有调用它的功能。如果你需要指定的参数,使用包装函数表达式:

User.findByEmail(email) 
    .then(function(user) { 
     return compare(/* magic */); 
    }).… 
+1

感谢Bergi,我更新了我的代码,并且工作... – rizidoro

4

我做了什么BERGI说,对我的作品:

this.findByEmail(email) 
.then(function(user) { 
    return compare(password, user.password); 
})