node.js -bcrypt.compare()在异步函数中不起作用
这是我的代码:
async function(account, pwd, done) {
Member.findOne({ account: account }, (err, user) => {
if (err) {
return done(err);
} else if (await bcrypt.compare(pwd, user.pwd)) {
return done(null, user);
} else {
return done(null, false, { message: 'Password incorrect' });
}
});
}
但是当我运行服务器时,我总是会遇到此错误:
E:\nodeJS\Express\server\routes\api\login.js:22
} else if (await bcrypt.compare(pwd, user.pwd)) {
^^^^^^
SyntaxError: Unexpected identifier
有人知道如何修复它吗? 谢谢您的回答!
Here is my code:
async function(account, pwd, done) {
Member.findOne({ account: account }, (err, user) => {
if (err) {
return done(err);
} else if (await bcrypt.compare(pwd, user.pwd)) {
return done(null, user);
} else {
return done(null, false, { message: 'Password incorrect' });
}
});
}
But when I run the server, I always get this error:
E:\nodeJS\Express\server\routes\api\login.js:22
} else if (await bcrypt.compare(pwd, user.pwd)) {
^^^^^^
SyntaxError: Unexpected identifier
Does anyone know how to fix it?
Thank you for answering!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下操作:
比较同步无需等待异步功能。
Try this:
compareSync works for async functions without an await.
我找到了解决方案!只需更改
async
的位置即可。解决方案如下:
将
async
放在(err, user) =>......
前面再次感谢那些花时间回答我的问题的人!
I found the solution! Just change the location of
async
.Here is the solution:
Put
async
in front of(err, user) =>......
Thank again for those who spend time on my question!