I'm fairly new to Angular 9, and essentially I am wondering how do I return an array of objects from an async source? I currently have a firestore query in which the documents that are returned have a reference whose data I wish to render.
ngOnInit
this.currentUserRef.collection('links', ref => ref.where('pendingRequest', '==', false)).get()
.pipe(map(querySnap => {
const ret = [];
querySnap.forEach(doc => {
const val = doc.get('otherUser').get().then(userData => {return userData});
ret.push(val);
});
return ret;
})).subscribe(val => {
this.links = val;
});
HTML
<ion-item *ngFor="let link of links | async">
<ion-avatar slot="start">
<img [src]="getImage(link.get('profilepic'))">
</ion-avatar>
<ion-label>
<h2>{{link.get('name')}}</h2>
<p>{{link.get('bio')}}</p>
</ion-label>
<ion-checkbox id="{{link.id}}"></ion-checkbox>
</ion-item>
This currently returns the error: InvalidPipeArgument: '[object Promise]' for pipe 'AsyncPipe' And when I remove the async pipe in the HTML it returns a zoneawarepromise.
EDIT
this.links = this.currentUserRef.collection('links', ref => ref.where('pendingRequest', '==', false)).get()
.pipe(map(querySnap => {
const ret = [];
querySnap.forEach(async doc => {
const val = await doc.get('otherUser').get().then(userData => {return userData});
ret.push(val);
});
return ret;
}));
This now works however I don't know how efficient and scalable the solution is for a large number of documents due to the
ret array
Note: querySnap is an object that has a built-in forEach method to loop through the array in it's docs property.