简体   繁体   English

未处理的拒绝(typeError):无法在 google chrome mobile 上使用 reactjs axios 读取未定义的属性

[英]Unhandled Rejection (typeError): Cannot read property of undefined using reactjs axios on google chrome mobile

When inputting incorrect information in my google chrome browser in my laptop I get the error response here's a screenshot of the response in my laptop browser using this code在笔记本电脑的 google chrome 浏览器中输入不正确的信息时,我收到错误响应,这是使用此代码在笔记本电脑浏览器中的响应屏幕截图

axios
        .post(`${strapi}/auth/local`, loginData)
        .then(res => {
          if (undefined === res.data.jwt) {
            this.setState({error: res.data.message, Loading: false});
            return;
          }

          sessionStorage.setItem('JWT', res.data.jwt);
          sessionStorage.setItem('username', res.data.user.username);
          sessionStorage.setItem('role', res.data.user.role.type);


          this.setState({Loading: false, token: res.data.jwt, username: res.data.user.username, userEmail: res.data.user.email, isLoggedIn: true});
          // window.location.href = '/';
        })
        .catch(err => {
          this.setState({
            error:err.response.data.message[0].messages[0].message
          })
          console.log(this.state.error)
        })

however when logging in to my mobile chrome browser using the same lline of code this appears mobile screenshot但是,当使用相同的代码行登录我的移动 chrome 浏览器时,会出现移动屏幕截图

how to fix this issue?如何解决这个问题?

This type of error occur when you are not getting your error.response.当您没有收到 error.response 时,就会发生这种类型的错误。

So, what you can do is you can set default message if you get any unknown errors.因此,如果您遇到任何未知错误,您可以设置默认消息。

     let MESSAGE;

     if(error.response){
         MESSAGE = err.response.data.message[0].messages[0].message
     }else{
          MESSAGE = 'Something went wrong.'
     }

     this.setState({
         error: MESSAGE
      })

Or else you can log your error why this happen否则你可以记录你的错误为什么会发生这种情况

 console.log('error',error) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的拒绝(TypeError):尝试在 ReactJS 上使用 Axios 加载 API 数据时,无法读取未定义的属性“0” - Unhandled Rejection (TypeError): Cannot read property '0' of undefined happening while trying to load API data with Axios on ReactJS axios:未处理的拒绝(TypeError):无法读取未定义的属性“错误” - axios: Unhandled Rejection (TypeError): Cannot read property 'error' of undefined ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送” - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined Reactjs + Redux 未处理的拒绝(TypeError):无法读取未定义的属性“数据” - Reactjs + Redux Unhandled Rejection (TypeError): Cannot read property 'data' of undefined ReactJS - 未处理的拒绝(TypeError):无法读取未定义的属性“id” - ReactJS - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送”。 推送到另一个组件时遇到问题 - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined. Having issues pushing to another component 错误:未处理的拒绝(TypeError):无法读取未定义的属性“livres”,ReactJS,搜索 - Error: Unhandled Rejection (TypeError): Cannot read property 'livres' of undefined, ReactJS, Search 未处理的拒绝(TypeError):无法读取未定义的属性“子级” - Unhandled Rejection (TypeError): Cannot read property 'children' of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“ city” - React- Unhandled Rejection (TypeError): Cannot read property 'city' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM