【发布时间】:2018-01-12 20:43:36
【问题描述】:
我正在尝试在我的 mocha 测试的 before 块中创建一个测试用户,我需要对其密码进行哈希处理。 Apparently you don't need a done() function if you use promises。我正在实现 bcrypt.hash,就像它在 bcrypt 文档中所说的那样:
before(function(){
//create a user and populate user's first recipes
//create a User object
return bcrypt.hash('newt', 10).then(function(err,hash){
//create the user object
var user = new User({email:'test@test.io',username:'test',password:hash})
//save that user
user.save()
})
})
但是,当我运行测试时,它给了我一个错误:
TypeError: Cannot read property 'then' of undefined
什么给了?谢谢。
【问题讨论】:
-
看起来您的
bcrypt没有名为hash的函数。 -
你能完全展示你的
before钩子吗? -
bcrypt.hash() 肯定存在,事实上,如果我删除承诺并将 .then() 函数作为一个简单的回调函数,它就可以正常工作。只是当我添加 .then() 时它才会中断。
标签: node.js promise mocha.js bcrypt