'Why is JSON data sent from server to browser undefined?

I want to make a request to my server with fetch(), and have data returned to be used in the front end app.

here is my route:

    app.get('/game-data', (req, res) => {
    res.json({ data: "test-data" })
})

and here is my request:

button.addEventListener('click', () => {
    fetch('/game-data', {
        headers: {
            'accept': 'application / json',
            'Content-Type': 'application/json'
        }
    })
        .then(response => {
            console.log(response)
            response.json()
        })
        .then(myJson => {
            console.log(myJson)
        })
})

I can see the response object in the first console log, but response.json(), or response.text() are returning undefined.

Please help me see what I am missing!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source