【问题标题】:App crushes on bad request and network error using fetch in react-native应用程序在 react-native 中使用 fetch 处理错误请求和网络错误
【发布时间】:2021-12-27 10:24:09
【问题描述】:

当 fetch 返回错误请求或网络错误时,我的 react 本机应用程序在 .apk 中崩溃 下面是获取函数:

    try {
      const reponse = await fetch(
        'http://example.com',
        {
          method: 'POST', // *GET, POST, PUT, DELETE, etc.
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
          },
          body
        },
      );
      if (reponse.ok) {
        const data = await reponse.json();
        console.warn('Success response', data);
        return navigation.navigate('different', {
          token: data.token,
          memberNo: data.user.memberno,
        });
      } else {
        setStatus('Incorrect Details entered');
        // console.warn('Failed response', reponse);
      }
    } catch (error) {
      setStatus('Network request failed connect to the internet');
      // console.error('CATCH Error', error);
    }
  }```

【问题讨论】:

  • 你试图解决什么崩溃?查看文档并对错误请求和fetch 函数进行一些研究?

标签: react-native fetch


【解决方案1】:

首先检查您的 API 响应可能是它返回的内容为 null 或未定义,并且您在屏幕中使用了它。 例如,当我们使用 flatlist 并传递 undefined 和 null 时,apk 会崩溃,因此请关注您的数据格式

【讨论】:

  • 谢谢。因为我错误地使用了 UseEffect 挂钩,所以 fetch 函数不是问题
猜你喜欢
  • 2020-11-20
  • 1970-01-01
  • 2017-12-05
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
相关资源
最近更新 更多