【问题标题】:Possible Unhandled Promise Rejection (id:0) Error: network error in react native可能的未处理承诺拒绝(id:0)错误:本机反应中的网络错误
【发布时间】:2018-07-30 09:13:50
【问题描述】:

可能出现未处理的 Promise Rejection (id:0) 错误:react native 中的网络错误

   import React, {Component} from 'react';
   import {View,Text} from 'react-native';
   import axios from 'axios';

   class AlbumList extends Component {
    state = {albums: []};  
   componentWillMount() {
    axios.get('https://rallycoding.herokuapp.com/api/music_albums')
    .then(response=>this.setState({albums: response.data}));
}
renderAlbums() {
return this.state.albums.map(album => <Text>{album.title}</Text>);
}    

render() {
    console.log(this.state);
return(
<View>
  {this.renderAlbums()}
</View>
);
}
};

export default AlbumList;

// 我无法从给定的链接加载我的数据,在调试模式下也只有一个 // 创建了一个具有空值的状态,并且在 //console 中看不到其他数据,所以请帮助我获取数据

这是我遇到的错误的屏幕截图

【问题讨论】:

  • 你能不能试试同样的fetch看看

标签: android react-native android-emulator general-network-error


【解决方案1】:

我认为您应该尝试重新加载应用程序/重新安装应用程序/重新启动模拟器/尝试新的模拟器。我也有多个通用的network errors,只使用普通的fetch,它们都与只是模拟器很奇怪有关。

【讨论】:

    【解决方案2】:

    我这样做了,我正在获取数据。

     componentWillMount() {
      await fetch("https://rallycoding.herokuapp.com/api/music_albums")
      .then(response => response.json())
      .then(data => {
    
        console.log("data" + JSON.stringify(data))
      });
    
     }
    

    【讨论】:

    • 抱歉,这显示“开发服务器返回响应错误代码:500 并在控制台中返回 GET(内部服务器错误)和未捕获(在承诺中)错误。
    • @MayankSharma,我得到的响应与在浏览器上点击 URL 时得到的响应完全相同,我认为这就是您所需要的。您能否详细说明您遇到了什么错误以及您做了什么?
    • 您是否关闭了终端(本地服务器)并重新启动了它?
    【解决方案3】:

    这肯定不是回答你的问题,而是我看到的第一件事:

    renderAlbums() {
        return this.state.albums.map(album => <Text>{albums.title}</Text>);
    } 
    

    应该是

    renderAlbums() {
        //In map you have to use album, not albums.
        return this.state.albums.map(album => <Text>{album.title}</Text>);
    }
    

    【讨论】:

    • 应该是:- return this.state.albums.map(album => {album.title});
    【解决方案4】:

    我试过了,它工作正常:

    axios.get('https://rallycoding.herokuapp.com/api/music_albums')
                .then(response => {
                    this.setState({albums: response.data});
                    //console.log(this.state.albums);
                    console.log(response);
                })
                .then(error => console.log(error));
    

    【讨论】:

      【解决方案5】:

      就我而言,我使用了下面的代码

      export const GetOP = async sCode => {      
        const config = {
          url: "http://192.168.231.105:3000/api/ApiOps/",
          params: {
            sCode: "UAR0348"
          }
        };
      
        let res = await axios(config);
      
        console.log(res.data);
      
        return res.data;
      };
      

      这个http://192.168.231.105:3000/api/ApiOps/ 是 IIS 中的 asp.net mvc 和主机。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-09
        • 2019-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-26
        • 1970-01-01
        相关资源
        最近更新 更多