【问题标题】:I can't handle this error internal/errors:464我无法处理此错误内部/错误:464
【发布时间】:2022-09-27 21:30:22
【问题描述】:

当我尝试在 express 中进行身份验证时,会出现以下错误。但是如果成功登录没有错误。

这是我的代码:

exports.loginUser = async (req, res) => {
try {
    const user = await User.findOne({ username: req.body.username });
    !user && res.status(400).json(\"Wrong credentials!\");
    
    const validated = await bcrypt.compare(req.body.password,user.password);
    !validated && res.status(400).json(\"Wrong credentials!\");
    
    const { password, ...others } = user._doc;
    res.status(200).json(others);

    
} catch (err) {
    res.status(500).json(err);
}

};

这是错误: Server crush

    标签: node.js express authentication


    【解决方案1】:

    在 res.status(400).json() 之后,您应该键入 return 以便不会执行其余代码。

    exports.loginUser = async (req, res) => {
    try {
    const user = await User.findOne({ username: req.body.username });
     if (!user) {
       res.status(400).json("Wrong credentials!")
      return;
     }
    
    const validated = await 
        bcrypt.compare(req.body.password,user.password);
       if (!validated) {
       res.status(400).json("Wrong credentials!")
      return;
      }
    
    const { password, ...others } = user._doc;
    res.status(200).json(others);
    
    
    } catch (err) {
       res.status(500).json(err);
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      • 2011-02-08
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多