1

I want to get a json file in return.

this is a fetch Function i am using

handleApi(){
    return 
        fetch('https://facebook.github.io/react-native/movies.json')
            .then((response) => response.json())
                .then((responseJson) => {
            return responseJson.movies;
        })
}

this function is being called on an button click event.

handleSubmit() {
    console.log(
        this.handleApi();
    )

but i am getting this Promise object in return not expected data

Promise {_40: 0, _65: 0, _55: null, _72: null}_40: 0_55: null_65: 0_72: null__proto__: Object

Tauqeer Hassan
  • 109
  • 2
  • 13

2 Answers2

5

A more simplified

handleApi(){
    return 
        fetch('https://facebook.github.io/react-native/movies.json')
            .then(response => 
                response.json().then(jsonObj => return jsonObj.movies)
            )
}

then in handleSubmit

handleSubmit() {
    this.handleApi().then(movies => {
        console.log('Print list of movies:', movies);
    });
)
digit
  • 4,479
  • 3
  • 24
  • 43
1

Please update your code as following:

handleApi(){
return 
    fetch('https://facebook.github.io/react-native/movies.json')
        .then((response) => response.json())
        .then((responseJson) => {
            return responseJson.movies;
        })
}

and

handleSubmit() {
 this.handleApi().then(function(movies) {
    console.log('Print list of movies:', movies);
 });
}
Rohan Kangale
  • 931
  • 4
  • 11
  • 29