我正在尝试生成一个 ipfs 节点并将文件上传到 ipfs。但是 ipfs.add() 方法总是抛出错误“multihash must be a buffer”。谷歌搜索表明节点的 multihash 包中的 isBuffer 检查是问题,但没有提供解决方案。
但是当我使用 ipfs-http-client 连接到在本地运行的 ipfs 节点时,同样的方法也有效。仅当我尝试生成节点时才会发生此错误。
这是上传文件的代码
async uploadFile() {
const IPFS = require('ipfs')
const ipfs = await IPFS.create()
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => {
const buffer = Buffer.from(reader.result)
ipfs.add({content: buffer})
.then(files => {
resolve(files)
})
.catch(error => reject(error))
}
reader.readAsArrayBuffer(this.state.selectedFile)
})
}
这是控制台中抛出的错误
Uncaught (in promise) Error: multihash must be a Buffer
at Function.validateCID (0.chunk.js:58311)
at new CID (0.chunk.js:58143)
at new CID (0.chunk.js:58476)
at persist (0.chunk.js:157864)
at :3000/async http:/localhost:3000/static/js/0.chunk.js:156829
at async parallelBatch (0.chunk.js:183162)
at async buildFileBatch (0.chunk.js:156903)
at async batch (0.chunk.js:182292)
at async reduceToParents (0.chunk.js:156774)
at async balanced (0.chunk.js:156768)
at async all (0.chunk.js:182254)
at async fileBuilder (0.chunk.js:157010)
at async parallelBatch (0.chunk.js:183162)
at async treeBuilder (0.chunk.js:157803)
at async push../node_modules/ipfs-unixfs-importer/src/index.js.module.exports (0.chunk.js:157711)
at :3000/async http:/localhost:3000/static/js/0.chunk.js:119802
at :3000/async http:/localhost:3000/static/js/0.chunk.js:119828
at :3000/async http:/localhost:3000/static/js/0.chunk.js:119843
at async addAll (0.chunk.js:119787)
at async last (0.chunk.js:182560)
at async IPFS.add (0.chunk.js:120025)