这是项目中与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的报错,不知道是写法太旧了还是其他地方有问题,在此恳请大家指导
先 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!')