【问题标题】:Unhandled promise rejection: TypeError: Network request failed expo node backend未处理的承诺拒绝:TypeError:网络请求失败的博览会节点后端
【发布时间】:2020-09-13 19:17:04
【问题描述】:

我有一个我的 expo 应用程序正在查询的节点后端。 node-express-mongo 后端工作得非常完美,我可以使用 Postman 的 GET 请求进行验证,但我的应用程序中出现未处理的承诺拒绝网络失败错误

完全错误:

[Unhandled promise rejection: TypeError: Network request failed]
- node_modules/whatwg-fetch/dist/fetch.umd.js:473:29 in xhr.onerror
- node_modules/event-target-shim/dist/event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:574:29 in setReadyState
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:190:12 in emit
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:436:47 in __callFunction
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:26 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:384:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

这是我的代码:

api.js

export const fetchMeetups = () =>
    fetch('http://localhost:3000/api/meetups')
        .then(res => res.json());

App.js

import * as React from 'react';
import { Platform, StyleSheet, Text, View, ActivityIndicator } from 'react-native';
import { fetchMeetups } from './constants/api'; 

const instructions = Platform.select({
  ios: `Press Cmd+R to reload,\nCmd+D or shake for dev menu`,
  android: `Double tap R on your keyboard to reload,\nShake or press menu button for dev menu`,
});


class App extends React.Component{
  static defaultProps = {
    fetchMeetups
  }

  state = {
    loading: false,
    meetups: []

  }

  async componentDidMount(){
    this.setState({loading: true});
    const data = await this.props.fetchMeetups();
    setTimeout(() => this.setState({loading: false, meetups: data.meetups}),2000)
  }


  render(){
    if(this.state.loading){
      return(
        <ActivityIndicator size="large"/>
      )
    }
    return (
      <View style={styles.container}>
        <Text>MeetupME</Text>
      </View>
    );
  }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

index.js

import { registerRootComponent } from 'expo';

import App from './App';

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in the Expo client or in a native build,
// the environment is set up appropriately
registerRootComponent(App);

-------------更新------ 添加了一个 catch 块来获取并获取这个新错误

Network request failed
- node_modules/whatwg-fetch/dist/fetch.umd.js:473:29 in xhr.onerror
- node_modules/event-target-shim/dist/event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:574:29 in setReadyState
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:190:12 in emit
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:436:47 in __callFunction
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:26 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:384:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

【问题讨论】:

    标签: android react-native fetch expo


    【解决方案1】:

    在 React-native Expo 和 Python Django 后端有同样的问题。 问题是关于模拟器本地主机和服务器本地主机之间的冲突。 您的后端服务器可能在 127.0.0.1:8000 上运行,但模拟器找不到。

    在终端中使用命令“ipconfig”找到您的 IPv4 地址。 例如,它将是 192.138.1.40

    在此之后将其放入您的 fetch ( 'http://192.138.1.40:8000/')。
    还有一点也很重要 - 使用相同的主机和端口运行您的后端服务器。
    以 python Django 为例:

    py manage.py runserver 192.138.1.40:8000

    在 Django 上,您还需要在 settings.py 中添加 ALLOWED_HOSTS = ['192.138.1.40']

    【讨论】:

      【解决方案2】:

      在阅读了有关此问题的几个小时后,我发现这是 Expo 中的一个错误

      要返回,我清除了 Android 上的 Expo 缓存

      【讨论】:

        【解决方案3】:

        在您的 url 中代替 localhost,http://localhost:3000/api/meetups,提供您的 IP 地址,例如 http://192.168.49.108:3000/api/meetups

        【讨论】:

        • 那行不通。 localhost:3000/api/meetups 是节点进程
        • 现在也在浏览器中...这个地址不返回我的 json...我仍然在 expo 模拟器应用程序中遇到未解决的 promise 错误
        • 在终端运行 - adb reverse tcp:8163 tcp:8163 并尝试
        • no 没有帮助...我认为 fetch 存在一些问题...因为它没有从 fetch 获取 json 对象...它应该从 localhost:3000/api/meetups 获取。 ...就像邮递员的情况
        • 当我让 localhost:3000/api/meetups 获取请求时......它也在浏览器中返回我的 json......所以我认为它在后端部分工作......也是反应部分也是正确的...唯一不工作的部分.... const data = await this.props.fetchMeetups();
        猜你喜欢
        • 2021-04-12
        • 1970-01-01
        • 2020-07-12
        • 1970-01-01
        • 2021-06-25
        • 2020-10-28
        • 2021-05-28
        • 1970-01-01
        • 2018-06-27
        相关资源
        最近更新 更多