我正试图上传一个图片到ipfs在我的苗条应用程序。这是我试图上传图像的代码:
const uploadFile = async (file) => {
const ipfs = await IPFS.create();
const result = await ipfs.add(file);
console.log(result)
}file是ArrayBuffer格式的。
但是,它返回的错误是:
(8) index.js:54 Uncaught (in promise) TypeError: callback is not a function
at Function../node_modules/peer-info/src/index.js.PeerInfo.create (index.js:54)
at GossipSub._onIncomingStream (index.js:174)
at Upgrader._onStream (upgrader.js:313)
at Mplex.onStream (upgrader.js:235)发布于 2020-04-20 19:45:52
您可以通过在for循环中使用ipfs.add来尝试添加文件:
for await (const result of ipfs.add(file)) {
console.log(result);
}更多信息在这里:https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#add
发布于 2020-04-21 00:56:31
谢谢你,Mionutm!
那很有帮助。问题是file格式不正确。我以为ipfs需要一个ArrayBuffer,所以我将file读入ArrayBuffer格式。
但是,我们可以通过访问ipfs的属性将上传的文件添加到ipfs中,如下所示:
const file = e.srcElement.files[0];
for await (const result of ipfs.add(file)) {
console.log(result);
}这解决了add <suspended>和invalid content错误。
尽管如此,TypeError: callback is not a function仍未解决,我认为这是node_modules产生的错误(不确定)。
https://stackoverflow.com/questions/61315367
复制相似问题