5 通过前端向IPFS添加内容时出现ipfs.add is not a function

这是项目中与ipfs相关的代码

var ipfs = require('ipfs-http-client');

function saveImageOnIpfs(reader) {
    return new Promise((resolve, reject)=> {
        let buffer = Buffer.from(reader.result);
        ipfs.add(buffer).then(res=> {
            console.log("res: ", res);
            resolve(res[0].hash);
        }).catch(err=> {
            console.error(err);
            reject(err);
        });
    });
}

function saveTextBlobOnIpfs(blob) {
    return new Promise((resolve, reject)=> {
        let buffer = Buffer.from(blob, 'utf-8');
        ipfs.add(buffer).then(res=> {
            console.log("res: ", res);
            resolve(res[0].hash);
        }).catch(err => {
            console.error(err);
            reject(err);
        });
    });
}

在前端页面提交信息后就会出现Uncaught (in promise) TypeError: ipfs.add is not a function的报错,不知道是写法太旧了还是其他地方有问题,在此恳请大家指导

请先 登录 后评论

最佳答案 2022-04-28 16:05

先 create 出来一个实例,在用实例调用 add, 示例:

import { create } from 'ipfs-http-client'

// connect to the default API address http://localhost:5001
const client = create()

// connect to a different API
const client = create('http://127.0.0.1:5002')

// connect using a URL
const client = create(new URL('http://127.0.0.1:5002'))

// call Core API methods
const { cid } = await client.add('Hello world!')
请先 登录 后评论

其它 0 个回答

相似问题