ipfs.add is not working for ipfs client v44.3.0 how can I resolve this?

久未见 提交于 2020-07-09 12:06:41

问题


This is my code in App.js, and its always returning an "Unhandeled Rejection Type error saying that ipfs.add(...). then is not a function.

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;

Do I need to add an async function or something, I'm fairly new to js so any help would greatly appreciated. I just don't know how to change the ipfs.add to make my code work.

来源:https://stackoverflow.com/questions/62649387/ipfs-add-is-not-working-for-ipfs-client-v44-3-0-how-can-i-resolve-this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!