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?

2
  • 3
    add await response.json() to get the data. Commented May 24, 2021 at 18:56
  • That's a Response object. Commented May 24, 2021 at 19:03

1 Answer 1

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();
  }, []); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.