未处理的承诺拒绝:TypeError: undefined is not an object (evaluating '_context.t0.data.error')]

时间:2021-03-22 11:23:23

标签: javascript react-native

我收到以下错误:

"[Unhandled promise rejection: TypeError: undefined is not an object 
(evaluating '_context.t0.data.error')]
screens\Hospitais.js:65:20 in Dermatologistas#getUsers"

在我的应用中,你能帮我吗?

export default class Dermatologistas extends Component{
state = {
    errorMessage: null,
    users: []
  }

你如何看到消息说错误在这里

 getUsers = async() => {
   try{
     console.log('1')
     const response = await api.get('/auth/list')

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

async componentDidMount(){
  this.getUsers()
}

render(){
  const users = this.state.users
  console.log(users)
  return(
    <View style={styles.container}>
      {!!this.state.errorMessage && <text>{this.state.errorMessage}</text>}
      {this.state.users.filter(user => user.speciality === 'Hospital').map(user => (
          <View key={user._id} style={{marginTop: 15, alignItems: 'center'}}>
            <Text style={{fontWeight: 'bold', fontSize:20}}>{user.title}</Text>
            <Text>{user.speciality}</Text>
            <Button   title = 'View Profile'onPress ={() => this.props.navigation.navigate('Profile')}/>
            </View>
        ))}

    </View>
  )
}
  

}

这是我的 api 页面:

import {create} from 'apisauce'
import {AsyncStorage} from '@react-native-community/async-storage'


const api = create({
    baseURL: 'https://wonderful-skunk-63.loca.lt'
})

在我添加它之前,它按预期工作,只是显示没有提供令牌并且在其他页面中工作而无需身份验证

api.addAsyncRequestTransform( request => async() => {
    const token = await AsyncStorage.getItem('@backend:token')

    if (token)
        request.headers['Authorization'] = `Bearer ${token}`
})

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

export default api;

0 个答案:

没有答案