Firebase-如何等待getDownloadURL



我有一个函数可以正确地从文档中检索数据。但是,一个图像的URL已经是它的字段了。另一个图像只有一个firebase图像参考。在继续执行另一个功能之前,我需要等待下载URL被获取。我在下面尝试过,但运气不佳,我也不完全确定我是否将async固定在了正确的位置。

getPhoto(user_id: string) {
this._subscription = this._activatedRoute.params.pipe(
switchMap(params => {
return this.service.getPhoto(params);
})
).subscribe(async(result) =>  {
const imageOne = result.imageOne;
// Need to await the download URL here
const imageTwo = this.blah(result.imageTwoRef)
this.otherFunction(imageOne, imageTwo)

});
}

blah(reference){
var storage = firebase.storage();
var imageTwo = reference;
var imagePathRef = storage.ref().child(imageTwo);  
imagePathRef.getDownloadURL().then((url) => {
return url;
}); 
}

使用async关键字只对函数有效,这样做会返回promise。所以在这种情况下你的用法是正确的
只能在异步函数中以及promise调用旁边使用await。它将停止执行,直到你的承诺得到解决
我想你差不多完了。这样试试,让我知道:

getPhoto(user_id: string) {
this._subscription = this._activatedRoute.params.pipe(
switchMap(params => {
return this.service.getPhoto(params);
})
).subscribe(async(result) =>  {
const imageOne = result.imageOne;
// Need to await the download URL here
const imageTwo = await this.blah(result.imageTwoRef)
this.otherFunction(imageOne, imageTwo);

});
}

async blah(reference){
var storage = firebase.storage();
var imageTwo = reference;
var imagePathRef = storage.ref().child(imageTwo);  
const url = await imagePathRef.getDownloadURL();
return url;
}

最新更新