Mayank Sharma
Mayank Sharma

Reputation: 11

Possible Unhandled Promise Rejection (id:0) Error: network error in react native

Possible Unhandled Promise Rejection (id:0) Error: network error in 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;

// I can't load my data from the given link, also in debugging mode there's only //one state created which has empty values and no other data can be visible in //console so help me in fetching the data

Here is screenshot of error I'm getting

enter image description here

Upvotes: 0

Views: 18496

Answers (5)

For my case, I used this below code

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

This http://192.168.231.105:3000/api/ApiOps/ is asp.net mvc and host in IIS.

Upvotes: 0

AMIR KHOUTIR
AMIR KHOUTIR

Reputation: 11

I tried this , it's working normally :

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

Upvotes: 0

iuliu.net
iuliu.net

Reputation: 7145

I think you should try reload app/reinstall app/restart emulator/try on new emulator. I also have had multiple generic network errors using just regular fetch, and they were all related to just the emulator being weird.

Upvotes: 1

SuperSec
SuperSec

Reputation: 140

This surely not answer your question but the first thing I see :

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

should be

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

Upvotes: 0

Shubham Raitka
Shubham Raitka

Reputation: 1052

I did this, and I am getting the data.

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

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

 }

Upvotes: 0

Related Questions