React native fetch returns _bodyBlob _bodyInit headers etc instead of json with json()

After re-installing node_modules and using json() to my fetch call, I still receive an blob response. Here is my code:

export function test() {
  const url = 'http://url/api/testServlet';
  return fetch(url, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json' 
    }
  })
  .then((data) => {
        console.log("data ", data, " --- end data");
        data.json();
  })
  .catch((error) => {
    console.error(error);
  });
}

With console.log, I get:

data 
{
  "_bodyBlob": {
    blob
  }, 
  "_bodyInit": {
    blob
  }, 
  "headers": {
    headers
  }, 
  "ok": true, 
  "status": 200, 
  "statusText": undefined, 
  "type": "default", 
  "url": "http://url/api/testServlet"
}  
--- fin data

EDIT : I have also an error value.hasOwnProperty('tag') that comes from nowhere I have absolutely no idea from where that comes from... I have just a component that display the result of my API call and the code I showed above.

The message is:

"Error: value.hasOwnProperty is not a function. (In 'value.hasOwnProperty('tag'), 'value.hasOwnProperty' is undefined)

I read some subjects and all was talking about this error but there was the function hasOwnProperty written in there code. Not me...

Upvotes: 0

Views: 1891

Answers (1)

My bad. Or not. I don't know you tell me. I replaced by :

export function test() {
  return fetch('http://url/api/testServlet')
    .then((response) => response.json())
    .then((responseJson) => {
      console.log("data : ", responseJson)
      return responseJson;
    })
    .catch((error) => {
      console.error(error);
    });
}

And it worked. It may come from the const url I don't know but I'm happy. Thank you. Or thank me I don't know you tell me.

Upvotes: 3

Related Questions