-4

JSON RESPONSE AFTER .JSON Is Called

registerResponse is an API call for clarifaction

{
"person": {
    "name": "jeff",
    "state": "Florida",
    "city": "miami"
  },
  "food": "tacos",
  "drink": "water"
}
let response = registerResponse()
console.log(response.json().food)
console.log(response.json().person.state)

the first log input works but the second input throws an error. "cannot read property 'state' of undefined" is there a reason this is happening am i doing it wrong? I can remove state but then it just returns null. I'm suspecting the .json() method is not good with nested objects

Kevin
  • 3
  • 2

1 Answers1

-1

What is the registerResponse function? Assuming it is similar to fetch, calling .json() consumes the response body which means you can only call it once. You would need to save it to a variable:

const data = response.json()

console.log(data.food)
console.log(data.person.state)
bxvd
  • 22
  • 1
  • 2