可能的未处理的承诺拒绝(id:0)错误:React Native中的网络错误

时间:2018-07-30 09:13:50

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

可能的未处理的承诺拒绝(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;

//我无法从给定的链接加载数据,而且在调试模式下,仅创建一个状态为//的状态,该状态具有空值,并且在控制台中没有其他数据可见,因此请帮助我获取数据

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

enter image description here

5 个答案:

答案 0 :(得分:0)

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

 componentWillMount() {
  await fetch("https://rallycoding.herokuapp.com/api/music_albums")
  .then(response => response.json())
  .then(data => {

    console.log("data" + JSON.stringify(data))
  });

 }

答案 1 :(得分:0)

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

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>);
}

答案 2 :(得分:0)

我认为您应该尝试重新加载应用程序/重新安装应用程序/重新启动模拟器/在新模拟器上尝试。我也有仅使用常规network error的多个泛型fetch,它们都与怪异的模拟器有关。

答案 3 :(得分:0)

我尝试过,它正常工作:

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));

答案 4 :(得分:0)

对于我来说,我在下面的代码中使用了

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/是asp.net mvc,位于IIS中。

相关问题