DEV Community

Cover image for response.json is not a function TypeError
Aishanii
Aishanii

Posted on

response.json is not a function TypeError

If you are using fetch

The fetch() method returns a Promise that response to a Response object. The json() method basically parses the response for JSON which is then changed to a native JavaScript object.

Use the json method on response with correct call to fetch.

useEffect(() => {
    fetch("https://jsonplaceholder.typicode.com/todos")
      .then((response) => response.json())
      .then((data) => {
       console.log(data)
       }
 }));
Enter fullscreen mode Exit fullscreen mode

If you are using axios

Axios keeps parsing the response in check, so we just look into the data property of response as it contains the data sent from the server.

import axios from 'axios';

useEffect(() => {
    axios.get("https://jsonplaceholder.typicode.com/todos")
    .then((response) => {
        const ex = response.data.json();
        console.log(ex);
     });
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)