Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_context.t0.data.error') in expo

Viewed 9602
 export default class Signin extends Component {

     state ={
         errorMessage: null
     }

here I use apisauce and credentials to test the connection

   signin = async() => {
       try{
         const response = await api.post('/auth/authenticate')({
             email:'teste93@teste.com',
             password:'123'
         })
    

         const {user, token} = response.data
         await AsyncStorage.multiSet([
             ['@backend : token', token]
             ['@backend : user',JSON.stringify(user) ]
         ])

the expo saysthat the error is here

       }catch(response){
         this.setState({errorMessage: response.data.error})
       }
    
   }

   render() {
      return (
       <View style={styles.Container}>
            { this.state.errorMessage && <Text>{ this.state.errorMessage }</Text> }
         <Button onPress={this.signin} title="Entrar"/>
       </View>
     );
   }
 }

here my services/api

 import { create } from 'apisauce'


 const api = create({
     baseURL: 'http://locahost:300'
 })

 api.addResponseTransform(response => {
     if (!response.ok) throw response;
     console.log(response)
 })

 export default api;
1 Answers

TypeError: undefined is not an object

This error usually indicates that you're trying to access a property of a non-object type. The value exposed in the catch block will be an Error object, which does not have a data property.

Change your catch block to the following.

} catch (error) {
  this.setState({errorMessage: error.message})
}
Related