【问题标题】:ipfs.add is not working for ipfs client v44.3.0 how can I resolve this?ipfs.add 不适用于 ipfs 客户端 v44.3.0 我该如何解决这个问题?
【发布时间】:2020-10-20 06:44:42
【问题描述】:

这是我在 App.js 中的代码,它总是返回“未处理的拒绝类型错误,说明 ipfs.add(...). then 不是函数。

import React, { Component } from 'react';
import './App.css';
var ipfsAPI = require('ipfs-http-client')
var ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol:'http'})
class App extends Component {
  saveTestBlobOnIpfs = (blob) => {
    return new Promise(function(resolve, reject) {
        const descBuffer = Buffer.from(blob, 'utf-8');
        ipfs.add(descBuffer).then((response) => {
        console.log(response)
        resolve(response[0].hash);
      }).catch((err) => {
        console.error(err)
        reject(err);
      })
    })
  }

  render() {
    return (
      <div className="App">
        <h1>IPFS Pool</h1>
        <input
          ref = "ipfs"
          style = {{width: 200, height: 50}}/>
        <button
          onClick = {() => {
            console.log("Upload Data to IPFS");
            let content = this.refs.ipfs.value;
            console.log(content);
            this.saveTestBlobOnIpfs(content).then((hash) => {
              console.log("Hash of uploaded data: " + hash)
            });
          }}
          style = {{height: 50}}>Upload Data to IPFS</button>
      </div>
    );
  }
}

export default App;

我是否需要添加一个异步函数或其他东西,我对 js 还很陌生,所以任何帮助都将不胜感激。我只是不知道如何更改 ipfs.add 以使我的代码正常工作。

【问题讨论】:

    标签: ipfs js-ipfs


    【解决方案1】:

    我也遵循了相同的教程并遇到了同样的问题。 ipfs.add 函数不再接受调用函数。更多信息在这里:https://blog.ipfs.io/2020-02-01-async-await-refactor/

    解决方案是将您的 saveTestBlobOnIpfs 函数转换为 async/await 函数。像这样:

    saveTestBlobOnIpfs = async (event) => {
        event.preventDefault();
        console.log('The file will be Submitted!');
        let data = this.state.buffer;
        console.log('Submit this: ', data);
        if (data){
          try{
            const postResponse = await ipfs.add(data) 
            console.log("postResponse", postResponse);
          } catch(e){
            console.log("Error: ", e)
          }
        } else{
          alert("No files submitted. Please try again.");
          console.log('ERROR: No data to submit');
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 2020-02-14
      • 2021-10-23
      • 1970-01-01
      • 2021-07-31
      • 2020-12-28
      相关资源
      最近更新 更多