【问题标题】:How do I show message on 403 client side if the email fails to send?如果电子邮件发送失败,如何在 403 客户端显示消息?
【发布时间】:2020-08-24 06:00:45
【问题描述】:

我正在尝试在电子邮件发送或失败时显示警报消息。如果失败,我会从后端得到 403。但不知道如何在客户端显示它。如果成功,我得到一个 200。现在,在失败的情况下,如果我 console.log(res)(如下所示),它显示

res is not defined

Uncaught (in promise) Error: Request failed with status code 403

在网络选项卡中,我得到了响应。没关系。

我只想在我的反应组件中显示它的客户端。

  handleClick = (id) => {
    axios
      .post(`http://localhost:3000/api/v1/users/send-post/${id}`)
      .then((res) =>
        console.log(res),
        res.status === 200 ? alert("Email sent"): null
      )
      .catch((err) => {
        if (err) {
          return alert("Sorry, Something went wrong")
        }
      })
    }

【问题讨论】:

    标签: javascript node.js reactjs express promise


    【解决方案1】:

    错误状态在err.response.status

    修改

    if (err) {
              return alert("Sorry, Something went wrong")
            }
    

    if (err) {
              return alert("Sorry, Something went wrong. Status "+ err.response.status)
            }
    

    作为参考,你检查这个链接https://stackoverflow.com/a/39153411/12680971

    【讨论】:

    • 是的。它在 catch 块中。谢谢。
    【解决方案2】:

    console.log 调用后有一个逗号 ,

    下一步:

    .then(res => {
       console.log(res);
       res.status === 200 && alert("Email sent");
    })
    

    或其他变体:

    .then(res =>
       console.log(res) ||
       res.status === 200 ? alert("Email sent") : null
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2010-10-23
      • 2022-01-13
      • 2021-05-23
      • 2012-07-07
      相关资源
      最近更新 更多