使用ipfs http-客户端,我可以使用以下方法成功地添加数据。console.log(added)返回一个带有path、cid和size键的对象。
但是,console.log(exists)行返回Object [AsyncGenerator] {}。
我希望能够检查数据字符串是否存在。这个是可能的吗?
import { create as ipfsHttpClient } from 'ipfs-http-client'
const ipfsClient = ipfsHttpClient('https://ipfs.infura.io:5001/api/v0')
const handleData = async (data) => {
const added = await ipfsClient.add(data)
console.log(added)
const exists = await ipfsClient.get(data)
console.log(exists)
}
handleData('hello world')发布于 2022-03-17 17:58:05
get方法返回AsyncIterable<Uint8Array>对象,这可能是您要打印的对象。要获得每个字节,您必须遍历它:
const cid = 'QmQ2r6iMNpky5f1m4cnm3Yqw8VSvjuKpTcK1X7dBR1LkJF'
for await (const buf of ipfs.get(cid)) {
// do something with buf
console.log(buf);
}如果您只关心数据是否存在,则只需在迭代器上调用next()方法,并检查是否存在null或错误。
const exists = (await ipfs.get(cid)).next() !== nullhttps://stackoverflow.com/questions/71416861
复制相似问题