【发布时间】:2019-07-25 02:28:40
【问题描述】:
我似乎找不到从异步值访问值的方法,因为它总是返回未定义,我想等待函数结束然后检索该值,但它不起作用...
async UploadFile(file): Promise<any> {
let ipfsId: any
const fileStream = fileReaderPullStream(file)
await this.ipfs.add(fileStream, { progress: (prog) => console.log(`received: ${prog}`) })
.then((response) => {
ipfsId = response[0].hash
console.log(ipfsId)
return ipfsId
//window.open("localhost:8080/ipfs/" + ipfsId);
//window.open("https://ipfs.io/ipfs/" + ipfsId);
}).catch((err) => {
console.error(err)
})
}
这是我的电话:
uploadFile(event) {
const fileSelected: File = event.target.files[0];
(async() => {
this.temp_ipfs_hash = await this.IPFS.UploadFile(fileSelected)
.then((response) => console.log(response))
console.log(this.temp_ipfs_hash)
})()
}
我想访问返回值,但它总是返回未定义或错误值... 有人知道我可以在这里尝试什么吗?
非常感谢您抽出宝贵时间! :)
编辑:我不知道发布图片是错误的,很抱歉,我已经更改了!对不起! :(
【问题讨论】:
-
总是发布你的代码而不是添加图片
-
更准确地说,将您的代码发布为文本,以便用户能够轻松地复制/粘贴它。请参阅this guide 格式化代码块
-
我已经改了,很抱歉,我不知道:(
标签: angular typescript asynchronous ipfs