reactjs - 如何通过检查reactjs中的错误来修复错误?

标签 reactjs error-handling

我有这样的代码片段:

(...).catch(e) => {
         if(e.response.status === 401) {
          console.log("Wrong username or password")
       } else {
           console.log(e.statustext)
}}

我有这样的错误:Unhandled Exception (TypeError): cannot read property status of undefined
我该如何解决?

最佳答案

catch块传递了一个Error对象,它不包含任何名为response的属性。

then()块内,检查状态码是否为401,如果是,则将消息"Wrong username or password"引发错误,并在catch块内,使用Error.prototype.message记录该消息

.then(response => {
    if (response.status === 401) {
       throw new Error("Wrong username or password");
    }
    ....
 })
 .catch(e) => console.log(e.message));

关于reactjs - 如何通过检查reactjs中的错误来修复错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62265698/

相关文章:

ios - React Native - 设备上未加载 Webview 文件

reactjs - 如何将索引传递到 rowRenderer 中?

batch-file - 具有时间戳和日志记录处理超时/断开连接/故障的批处理文件-Ping

powershell - Powershell复制项目退出代码1

css - React-Emotion - 使用 EmotionJS 为 div 设置动画

reactjs - 如何在提取中删除 URL(改为使用某种变量)?

javascript - 获取 403 : restricted_client when trying to login with Firebase

php - PHP的输入类错误处理?

ios - 1个重复的体系结构arm64的符号-Xcode-想法用尽

php - 当我调用 warning() 或 info() 时,如何防止 post 变量出现在 Yii2 日志中?