0

I always have a lot of trouble with fetch() because I look at documentation and examples and it looks like I am doing it right but it still doesn't work.

I am trying to do fetch here but it just comes out as "[object Response]"

I don't know what that is actually? On Postman the same query returns a JSON object just fine and this looks to me just like the fetch examples so really not sure what I am doing wrong.

try {
    let response = await fetch(
        `https://us-central....`,
        {
            method: 'post',
            mode: 'no-cors',
            body: JSON.stringify(customer)
        }
    )

    let data = await response.text()

    console.log(`Response: ${data}`)
    console.log(`Response: ${response}`)

} catch (e) {
    console.error('Response Error: ', e)

    return e
}

Actually I understand it is supposed to work with let data = await response.json() but this gives me an error saying there is no JSON

D Ags
  • 1
  • 2
  • 1
    you should try `console.log(response)` see this for more info https://stackoverflow.com/questions/4750225/what-does-object-object-mean – uditkumar01 Aug 31 '22 at 13:27
  • By using a template string like that, you're explicitly turning the object into a string. – Pointy Aug 31 '22 at 13:28
  • `response` is indeed an object. To get the data out of this object you can use `response.json()` or `response.text()` just like you do in your code. Accessing `response` directly the way you do doesn't achieve that. Forget about the whole `\`Response: ${response}\`` line and what it outputs. The reason that it doesn't show anything in `data` is because you have set `mode: 'no-cors'` which makes it impossible to access the response. – Ivar Aug 31 '22 at 13:47

0 Answers0