【问题标题】:How to read data from IPFS如何从 IPFS 读取数据
【发布时间】:2021-03-22 12:59:02
【问题描述】:

我使用 Pinata 将 JSON 文件上传到 IPFS。

链接:

https://gateway.pinata.cloud/ipfs/QmXjUdgnkLLKi3fvZjvnyVUQv4kHpgobjPo67FpyqXxPhe

JSON 文件:

{
"attributes" : [ {
  "trait_type" : "Breed",
  "value" : "Maltipoo"
}, {
  "trait_type" : "Eye color",
  "value" : "Mocha"
} ],
"description" : "Colorful pots",
"image" : "https://gateway.pinata.cloud/ipfs/QmTgqf6NY2BtsB24aU6vVmuEBunztua5vxwcCRBqwGjHLm",
"name" : "Pots"
}

当我调用合同时,我会返回 URL。使用以下代码时如何访问“image”键?

async function showNFT() {

try {

  const nft = await nftContract.methods.tokenURI("1").call()
  console.log(nft)
  // https://gateway.pinata.cloud/ipfs/QmXjUdgnkLLKi3fvZjvnyVUQv4kHpgobjPo67FpyqXxPhe

} catch (err) {
   console.log('Error:' + err);
}

}

非常感谢

【问题讨论】:

    标签: node.js web3 ipfs


    【解决方案1】:

    我找到了访问 JSON 数据的解决方案,方法是使用 fetch() 和 json()。

    const nft = await nftContract.methods.tokenURI("1").call()
    const response = await fetch(nft);
    
    if(!response.ok)
      throw new Error(response.statusText);
    
    const json = await response.json();
    console.log(json.image) 
    

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 2021-12-12
      • 2021-11-29
      • 2012-03-09
      • 2011-01-26
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多