【问题标题】:bcrypt is not returning anything NODEJSbcrypt 没有返回任何东西
【发布时间】:2020-06-09 17:56:31
【问题描述】:

这总是有用的,不知何故这对我不起作用:

我用来比较密码的东西:

router.post("/login", [
    check('email', 'Email is required').isEmail(),
    check('password', 'Password is required').not().isEmpty()
    ], async (req, res) => {
    const { email, password } = req.body;
    const errors = validationResult(req);
    if ( !errors.isEmpty() || !email || !password ) {
        return res.status(401).json({err: "Authentication failure"});
    };
    try {
        const user = User.findOne({ email: email });
        if(!user) {
            return res.status(401).json({err: "Invalid username or password"});
        }
        const isValid = await bcrypt.compare(password, user.password);  //// <-------HERE!!!!!
        console.log("Not reaching here")
        if (!isValidPassword) {
            return res.status(401).json({err: "Invalid username or password"});
        }
        //if valid password 
        res.status(200).send("SUCCESS");
    } catch (error) {
        res.status(500).json({err: "Internal server error"});
    }
})

有人可以帮帮我吗?这是我第一次被困在这里。提前致谢。

【问题讨论】:

  • 它是否发送内部服务器错误响应?你在 catch 块中试过console.log(error) 吗?
  • 谢谢,我想通了。

标签: javascript node.js ajax express es6-promise


【解决方案1】:

我在进行数据库查询时没有使用“等待”! 更正:

const user = await User.findOne({ email: email });

【讨论】:

    猜你喜欢
    • 2015-02-11
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2019-04-22
    • 1970-01-01
    • 2014-04-28
    • 2011-05-04
    相关资源
    最近更新 更多