javascript - IPFS 问题:无法使用 ipfs.add(file) 上传文件
问题描述
伙计们。几个月前,我为上传文件添加了 ipfs,一切正常,但一周前我遇到了问题,无法解决。我尝试将文件发送到 IPFS 并获取文件的哈希值。
像这样。连接ipfs:
ipfs = await Ipfs.create({
config: {
Bootstrap: [
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
'/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
]
}
});
console.timeEnd('IPFS Started');
并尝试发送文件
const {ipfs, ipfsInitError} = useIpfs({commands: ['id']});
const addToIpfs = async (file) => {
const hashOfFile = await ipfs.add(file);
return hashOfFile[0].path;
};
所以,当我尝试上传时,我有错误
Unhandled Rejection (TypeError): undefined is not an object (evaluating ‘hashOfFile[0].path’)
in hashOfFile return function
AsyncGenerator {_invoke: function, next: function, throw: function, return: function, Symbol(Symbol.asyncIterator): function}
``
earlier it was hash of file.
could you help me?
解决方案
在 GitHub 上,我得到了开发人员的回答
以前 ipfs.add 返回一个数组,这很好,除非您添加大量文件,在这种情况下您的进程可能会耗尽内存。现在它返回一个你发现的异步迭代器。注意你不需要做双重等待:
// instead of
for await (const inputFile of await ipfs.add({ path: 'randompath.txt', content: file })) {
// do:
for await (const inputFile of ipfs.add({ path: 'randompath.txt', content: file }))
它可以工作,但在 Safari 中仍然存在问题 - 崩溃
Unhandled Rejection (TypeError): null is not an object (evaluating 'ipfs.add')
推荐阅读
- talend - 使用 tFileInputDelimited 从 CSV 读取 int 值时出错错误:对于输入字符串:“10617,,,,”
- python - Python 3.7.2 Turtle 源文件夹特定的错误消息
- python - 如何使用pickle保存sklearn模型
- ninject - 什么是 NInjects 相当于 TryAdd
- java - JNA 中的 RasEnumConnections 函数返回不完整的数据。我做错了什么?
- javascript - HTML/CSS/JS - 模式弹出窗口的覆盖背景 - 问题
- java - 循环数组队列中余数操作数的意义是什么
- java - 如何在 JAVA (android) 中将变量设置为 0.001
- python - Python:如何检查两个字符串的匹配字符数?
- php - 使用正则表达式后如何从url获取值?