admin管理员组

文章数量:1397142

 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:'[email protected]',
             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;
 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:'[email protected]',
             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;
Share Improve this question edited Jan 8, 2021 at 22:33 Dharman 33.4k27 gold badges101 silver badges147 bronze badges asked Jan 8, 2021 at 22:08 Paulo AvilaPaulo Avila 3211 gold badge3 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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})
}

本文标签: