Catch块:无法读取未定义

时间:2017-08-30 19:29:56

标签: react-native error-handling react-native-android catch-block

我遇到了catch块无法正常运行并出错的问题。

当我添加NetInfo if-else块之后添加该块工作但添加后显示该错误时,出现此错误。 这是我在login.js中的代码

componentDidMount() {
const { navigate } = this.props.navigation;
NetInfo.isConnected.fetch().done((isConnected) => {
if ( isConnected )
{
  return fetch('http://10.42.0.1:8000/stocks/',
{
  method: "GET",
  headers: {
    'Authorization': `JWT ${"eyJhbGciOiJIUz1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InNhbSIsInVzZXJfaWQiOjYxLCJlbWFpbCI6IiIsImV4cCI6MTUwNDAzNDUzOX0.d3tbOUS_0L9RzkWA30DT8mv7v7j0XuxnRuI_luhuNzI"}`
  }
})
  .then((response) => response.json())
  .then((responseJson) => {
    if (!responseJson.post){
    navigate("Login");
  }else
  {
    let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.setState({
    isLoading: false,
    sa: ds.cloneWithRows(responseJson.post),
  });
  }
})
}else
{
  navigate("Login");
  Alert.alert(
    'Check Internet connection',
    )
}
 }).catch((error) => {
     console.log(error);
   })
}

enter image description here

我哪里错了?请帮忙......

1 个答案:

答案 0 :(得分:1)

Try changing your .done to a .then.

I'd also suggest extracting some of that logic into separate functions to make it easier on yourself to read..Lots of braces and parens everywhere!