0

I'm trying to fetch a message from /api pages on NextJS, how do I get the sent response from API to front end?

Example API:

export default function handler(req, res) {
  res.status(200).json({ name: 'John Doe' })
}

My API call on front end:

    useEffect(() => {

        fetch("/api/hello",{method: "GET"}).then((response) => {
            console.log(response)
        })

    }, [])

Response I'm getting:

enter image description here

Why is the name 'John Doe' not appearing here?

When I was on create-react-app, I fetch APIs with axios and console logging response.data gives all the data sent from res.send from backend, but here on NextJS, it doesn't appear on API response

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
Parzival
  • 93
  • 6
  • You need to call the appropriate method of the response object to get the data, and this is well covered both here on Stack Overflow and on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) – Jared Smith Dec 09 '22 at 13:35
  • @JaredSmith thanks so much bro! the first comment solved my problem. – Parzival Dec 09 '22 at 13:43

0 Answers0