Juno
Juno

Reputation: 299

How to read values from response object?

I am trying to read a value from a response object, but this

fetch("https://api.nft.storage/upload", options)
          .then((response) => response.json())
          .then((response) => console.log(response))
          .then((response) => {
            console.log(response.value.cid);
            }

... doesn't work. Although my console shows the object being sent:

enter image description here

.. I get this error:

Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'value')

1056 | .then((response) => response.json())
  1057 | .then((response) => console.log(response))
  1058 | .then((response) => {
> 1059 |   console.log(response.value.cid);

Upvotes: 0

Views: 1669

Answers (1)

Barmar
Barmar

Reputation: 782148

console.log() doesn't return anything, so response in the next .then() is undefined. Just do both in the same function.

fetch("https://api.nft.storage/upload", options)
  .then((response) => response.json())
  .then((response) => {
    console.log(response);
    console.log(response.value.cid);
  }
}

Upvotes: 4

Related Questions