fetch response.json() gives responseData = undefined

React Native

React Native Problem Overview


When using fetch:

  fetch(REQUEST_URL, {
      method: 'get',
      dataType: 'json',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    })
    .then((response) => 
      {
        response.json() // << This is the problem
      })
    .then((responseData) => { // responseData = undefined
        
        console.log(responseData);
     });
     }).catch(function(err) {
        console.log(err);
      })
     .done();

The following works works, do you know why? :

    JSON.parse(response._bodyText)

React Native Solutions


Solution 1 - React Native

The chaining response should look more like this, specifically the response.json part. Then you should get an Object back in console.log.

.then(response => response.json())
.then(response => {
    
    console.log(response)

}

Solution 2 - React Native

Fetch is a little hard to get your head around. I am new to this so dont shoot me down if flames here but response data is another promise and you need to return response data and then handle that promise with yet another then statement where you can finally log the response, also your are missing some return statements in your promises:

var makeRequest = function(){

    fetch('https://jsonplaceholder.typicode.com/posts/1', {
        method: 'get',
        dataType: 'jsonp',
        headers: {
           'Accept': 'application/json',
           'Content-Type': 'application/json'
        }
    })
    .then((response) => {
       return response.json() // << This is the problem
    })
    .then((responseData) => { // responseData = undefined
        addTestToPage(responseData.title);
        return responseData;
    })
  .catch(function(err) {
      console.log(err);
  })
}

function addTestToPage(textToAdd){
   var para = document.createElement("p");
   var node = document.createTextNode(textToAdd);
   para.appendChild(node);

  var element = document.getElementsByTagName("body")[0];
  element.appendChild(para);
}

makeRequest();

hope that helps see: https://jsfiddle.net/byz17L4L/

Solution 3 - React Native

Here's how it finally worked out in my case:

fetch('http://localhost:3001/questions', {
		method: 'GET',
		headers: {
		"Accept": "application/json",
		'Content-Type': 'application/json'
		}
	})
	.then(response => { return response.json();})
	.then(responseData => {console.log(responseData); return responseData;})
	.then(data => {this.setState({"questions" : data});})

	.catch(err => {
		console.log("fetch error" + err);
	});
}

Solution 4 - React Native

because you didn't return response.json() in the first then.

Solution 5 - React Native

import React, {useEffect} from 'react';

useEffect(() => {
    getRecipes();
  }, []);

  const getRecipes = async () => {
    const response = await fetch(
      `https://........`
    );
    const data = await response.json();
    console.log(data);

  • Use this method You can easily fatch data.

Solution 6 - React Native

fetch(weatherIng + zipCode +apiKey)
    	.then(response => response.json())

      .then(response => {
     console.log(response.main);
     this.setState({
       weather: ((response.main.temp * (9/5))-459.67).toFixed(0),
       humidity:((response.main.humidity * (9/5))-459.67).toFixed(0)
     })

It will think that you are trying to declare something if you don't enclose it in its own:

  .then(response => {
     console.log(response.main);
     }) . " around the this.setState

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris G.View Question on Stackoverflow
Solution 1 - React Nativesmoore4View Answer on Stackoverflow
Solution 2 - React NativeAndy StannardView Answer on Stackoverflow
Solution 3 - React NativeProjenixView Answer on Stackoverflow
Solution 4 - React NativenicoleView Answer on Stackoverflow
Solution 5 - React NativeKhadim H.View Answer on Stackoverflow
Solution 6 - React NativeGlossyLetusView Answer on Stackoverflow