Javascript post请求失败后不返回错误-axios、express、node.js

Javascript post请求失败后不返回错误-axios、express、node.js,javascript,node.js,reactjs,express,axios,Javascript,Node.js,Reactjs,Express,Axios,我正在尝试实现密码更改的验证,我遇到的问题是,如果出现错误,我不会从服务器返回errorMessage。我已经设法让它工作,并发送回密码更新后的响应。此外,我可以在后端将错误消息记录到console.log中,但它不会将带有错误消息的对象返回到前端 if (!currentPassword) { console.log("no current password"); return res .status(400) .json({

我正在尝试实现密码更改的验证,我遇到的问题是,如果出现错误,我不会从服务器返回errorMessage。我已经设法让它工作,并发送回密码更新后的响应。此外,我可以在后端将错误消息记录到console.log中,但它不会将带有错误消息的对象返回到前端

    if (!currentPassword) {
    console.log("no current password");
    return res
      .status(400)
      .json({ errorMessage: "Please confirm your current password" });
}
前面的代码如下所示:

  handleSubmit = (event) => {
event.preventDefault();
const authorization = localStorage.getItem("accessToken");

axios
  .put(
    `${process.env.REACT_APP_SERVER_URL}/settings/password`,
    this.state.user,
    {
      headers: {
        authorization,
      },
    }
  )
  .then((res) => {
    if (res.errorMessage) {
      console.log(res, "Unsuccessful password updated");
   
    } else {
      console.log("updating - res:", res);
      this.setState({
        user: res.data,
      });
    }
  })

  .catch((err) => {
    console.log(err, "ERROR");
  });
 };
每次出现错误时,我都不会登录实际的错误消息,但它会被捕获到。原因是什么


谢谢不是直接的
res
它可以在
res.data
下找到

使用

而不是

if (res.errorMessage) {

为了更好地理解,您需要
console.log(res)
。然后您就可以理解响应的结构了

我已经厌倦了使用res.data.errorMessage和Console登录,但它什么也没做。我仍然收到错误的POST请求,并返回一条catch err消息。我认为您使用的是
axios.put
检查您的后端路由方法
app.POST或app.put
它们都是put,我已经在下面提供了整个后端路由,您可以在浏览器开发人员面板中检查网络
控制台.log
?他们有任何错误或检查响应是否正确,您是指浏览器控制台吗?有关错误的更多信息:加载资源失败:服务器响应状态为400(错误请求)SettingsPage_security.jsx:62错误:请求失败,状态代码400位于createError(createError.js:16)位于settle(settle.js:17)位于XMLHttpRequest.handleLoad(xhr.js:62)“错误”我已经厌倦了使用res.data.errorMessage和Console登录,但它没有做任何事情。我仍然收到错误的POST请求,并且正在返回catch err messageOK,因此当前问题与您返回的errorMessage无关,整个axios事务都有问题。catch记录的确切错误消息是什么?VM662:1 PUT 400(错误请求)这与您尝试检查errorMessage无关-在这之前,route/api/axios调用会中断。如果没有更多的代码,就无法进一步调试IMO。端点/api/设置/密码是否存在?你真的到了吗?您需要将调试添加到路由和/或控制器中,以查看进展情况,检查服务器日志以查看请求及其被拒绝的原因。因此,路由成功更新了密码,我在前端得到响应,即密码已更新,因此上述代码中的else语句起作用,我可以console.log(“更新-res:,res)
router.put("/password", isLoggedIn, (req, res, next) => {
  const { currentPassword, newPassword, newPasswordConfirm } = req.body;

 

  User.findById(req.user._id)
    .then((user) => {
      bcrypt.compare(currentPassword, user.password).then((isSamePassword) => {
        if (!isSamePassword) {
          console.log(
            "Incorrect current password. To change your password try again!"
          );
          return res.status(400).json({
            errorMessage:
              "Incorrect current password. To change your password try again!",
          });
        }

        return bcrypt
          .genSalt(saltRounds)
          .then((salt) => bcrypt.hash(newPassword, salt))
          .then((hashedPassword) => {
            User.findByIdAndUpdate(
              req.user._id,
              { password: hashedPassword },
              { new: true }
            )
              .then((user) => {
                console.log("user's password successfully changed");
                res.status(200).json(user);
              })
              .catch((err) => {
                res.status(500).json({ errorMessage: err.message });
              });
          })
          .catch((err) => {
            res.status(500).json({ errorMessage: err.message });
          });
      });
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({ errorMessage: err.message });
    });
});
router.put("/password", isLoggedIn, (req, res, next) => {
  const { currentPassword, newPassword, newPasswordConfirm } = req.body;

 

  User.findById(req.user._id)
    .then((user) => {
      bcrypt.compare(currentPassword, user.password).then((isSamePassword) => {
        if (!isSamePassword) {
          console.log(
            "Incorrect current password. To change your password try again!"
          );
          return res.status(400).json({
            errorMessage:
              "Incorrect current password. To change your password try again!",
          });
        }

        return bcrypt
          .genSalt(saltRounds)
          .then((salt) => bcrypt.hash(newPassword, salt))
          .then((hashedPassword) => {
            User.findByIdAndUpdate(
              req.user._id,
              { password: hashedPassword },
              { new: true }
            )
              .then((user) => {
                console.log("user's password successfully changed");
                res.status(200).json(user);
              })
              .catch((err) => {
                res.status(500).json({ errorMessage: err.message });
              });
          })
          .catch((err) => {
            res.status(500).json({ errorMessage: err.message });
          });
      });
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({ errorMessage: err.message });
    });
});