【问题标题】:async bcrypt post request returns no response异步 bcrypt 发布请求不返回响应
【发布时间】:2020-09-19 06:46:46
【问题描述】:

我正在尝试比较密码以登录用户,但我不断收到“此请求没有响应。”

这是我的发帖路线。

router.route("/login/:username1").post(async (req, res) => {
  const userexist = User.find(username=>userexist.username=req.body.username1)
      if(userexist == null) 
      {
        return res.status(400).json("Cannot find user");
      }
      try 
      {
        if(await bcrypt.compare(req.body.password, userexist.password))
        {
          res.json("success");
        }
        else
        {
          res.json("Not Allowed");
        }
      } catch {
        res.status(500).json();
      }
});

这是我在登录页面的发布方法。

onSubmit(e) {
    e.preventDefault();
    const isValid = this.validate();
    const username1=this.state.username;
    if (isValid) {
      const user = {
        username: this.state.username,
        password: this.state.password,
      };

      axios
      .post("http://localhost:5000/users/login/"+ username1, user)
      .then((res) => console.log(res.data));
      this.setState(initialState);
    }
  }

【问题讨论】:

  • username=>userexist.username?也许你的意思是user => user.username
  • 我不这么认为,如果我这样做,我会尝试按用户名搜索,然后我不会得到在正文中传递的用户名。那将是一个空用户名仪式?我想也许它也与搜索参数有关。
  • userexist 在过滤器函数中未定义。所以正确的代码是const userexist = User.find (user => user.username == req.body.username1)。附:是的,还有一个错误:使用== 而不是= 进行比较。
  • 谢谢,但是当我发布请求时,它会中断 mongo db 连接。并且它不接受 == 来比较它需要 === 奇怪..用户不应该是用户名,因为它在我的架构中?

标签: node.js reactjs mongodb react-router bcrypt


【解决方案1】:

稍作修正,看看效果如何:

  • 在控制器中使用 await
  • 使用下面的代码行,您正在发送用户对象。所以在你的控制器中,使用 req.body.username (不是 req.body.username1)
axios.post("http://localhost:5000/users/login/"+ username1, user)
  • 更正您的find 函数,如代码 sn-p 所示
  • 还要检查您的 cors 并确保客户端遵守服务器 cors 规则
router.route("/login/:username1").post(async (req, res) => {
    const userexist = await User.find(user=>user.username==req.body.username) //<---- make correction to find method ... use await and use correct req body object i.e. username not username1
        if(userexist == null) 
        {
          return res.status(400).json("Cannot find user");
        }
        try 
        {
          if(await bcrypt.compare(req.body.password, userexist.password))
          {
            res.send("success"); //<----- as best practice, while sending strings just use res.send('success')... res.json is not reqd
          }
          else
          {
            res.send("Not Allowed"); //<----- for strings, it is better to use res.send instead of res.json
          }
        } catch {
          res.status(500).json(); //<----- remember to pass meaningful error message back to client
        }
  });

【讨论】:

    【解决方案2】:

    只要切开任何人都有同样的问题。 问题是user.password 它返回了一个必须像user[0]['password'] 那样访问的数组,它现在工作正常。我正在使用反应路由器,并且我通过 .env 文件连接了我的 MongoDB,该文件由我的 MongoDB models.js 架构使用。

    User.find({ username: req.body.username }).then(async (user)  => {
         if( user.length>0) {
             await bcrypt.compare(req.body.password, user[0]['password'], 
             function(err, result) {
    
             if(result){
                res.send('success') ;
              }else{
    
               res.send('not allowed') ;
             }
         });
      }
    

    【讨论】:

      猜你喜欢
      • 2021-05-21
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2015-04-07
      • 2019-05-07
      • 2017-12-07
      • 2019-09-20
      • 2020-04-20
      相关资源
      最近更新 更多