node.js -bcrypt.compare()在异步函数中不起作用

发布于 2025-01-20 06:46:31 字数 703 浏览 2 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2025-01-27 06:46:31

尝试以下操作:

const bcrypt = require('bcrypt');

bcrypt.compareSync(rawPassword, hashedPassword);

比较同步无需等待异步功能。

Try this:

const bcrypt = require('bcrypt');

bcrypt.compareSync(rawPassword, hashedPassword);

compareSync works for async functions without an await.

两个我 2025-01-27 06:46:31

我找到了解决方案!只需更改async的位置即可。
解决方案如下:

function(account, pwd, done) {
        Member.findOne({ account: account }, async(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' });
            }

        });
    }

async放在(err, user) =>......前面

再次感谢那些花时间回答我的问题的人!

I found the solution! Just change the location of async.
Here is the solution:

function(account, pwd, done) {
        Member.findOne({ account: account }, async(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' });
            }

        });
    }

Put async in front of (err, user) =>......

Thank again for those who spend time on my question!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文