1

This is the code snippet:

  useEffect(() => {
    async function fetchData() {
      const url = 'https://api.randomuser.me';
      const response = await fetch(url);
      console.log('response: ', response);
    }
    fetchData();
  }, []); 

as you can see, it is an useEffect hook, calling a public API for some data, using async await in order to wait for the response.

But the logged data isn't containing what it should.

This is in console.log:

body: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: ""
type: "cors"
url: "https://api.randomuser.me/"
__proto__: Response

what's wrong?

Leo Messi
  • 5,157
  • 14
  • 63
  • 125

1 Answers1

2

You need to parse the response in to JavaScript object, using response.json()


  useEffect(() => {
    async function fetchData() {
      const url = 'https://api.randomuser.me';
      const response = await fetch(url);
      const result = await response.json()
      console.log('result: ', result); // this the actual json response
    }
    fetchData();
  }, []); 
Naren
  • 4,152
  • 3
  • 17
  • 28