I'm trying to retrieve the download url for every image stored in Firebase Storage. The url is saved in Firebase Firestore. The following code maps the documents received from snapshotChanges method from Firebase Firestore. For every document I get the download url. But this method returns a promise object. Because of this, the map method finishes before the promise object is returned and this causes data.downloadUrl to be empty.
this.listingCollection = this.db.collection<Listing>('listings');
this.listings = this.listingCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Listing;
const id = a.payload.doc.id;
if (data.path) {
let storageRef = firebase.storage().ref();
let spaceRef = storageRef.child(data.path);
spaceRef.getDownloadURL().then((url) => {
data.downloadUrl = url;
console.log(url);
}).catch((error) => {
});
}
return { id, ...data };
});
});
I'm using the downloadUrl in the HTML-page like this.
<ng-container matColumnDef="downloadUrl">
<mat-header-cell *matHeaderCellDef></mat-header-cell>
<mat-cell *matCellDef="let element">{{element.downloadUrl}}</mat-cell>
</ng-container>
Please help. Thanks.
return
inside yourgetDownloadURL()
'sthen
callback? :spaceRef.getDownloadURL().then(url => { data.downloadUrl = url; console.log(url); return { id, ...data }; })
– JP Lew