简体   繁体   中英

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

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

how to fix this issue?

This type of error occur when you are not getting your 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM