【问题标题】:How to read values from response object?如何从响应对象中读取值?
【发布时间】:2022-01-14 17:48:46
【问题描述】:

我试图从响应对象中读取一个值,但是这个

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

... 不起作用。虽然我的控制台显示正在发送的对象:

.. 我得到这个错误:

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);

【问题讨论】:

    标签: javascript object response ipfs


    【解决方案1】:

    console.log() 不返回任何内容,因此下一个.then() 中的response 未定义。只需在同一个函数中执行两者即可。

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

    【讨论】:

    • 谢谢巴尔玛!很高兴上完这一课;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多