1

I am currently trying to figure out how to load an angular/fire-collection including all of its subcollections with RxJS.

This is my current approach:

return this.collectionRef.valueChanges().pipe(
      flatMap((entities: Entity[]) => entity),
      mergeMap((entity: Entity) => this.setSubCollection1(entity)),
      mergeMap((entity: Entity) => this.setSubCollection2(entity)),
      scan((entities: Entity[], entity: Entity) => entities.filter(a => a.id !== entity.id).concat(entity), [])
    );

and to load the documents in their subcollections

  private setSubCollection1 = (entity: Entity): Observable<Entity> => {
    return this.subCollectionRef.valueChanges(actor).pipe(
      map((subEntities1: SubEntity1[]) => {
        entity.subEntities1 = subEntities1;
        return entity;
      })
    );
  }

It works fine when having a full stream. But now I wanted to get all of data in one single Promise: I already tried .first().toPromise() but this only gets the first entry, and does not finish if the collection has no entries. Using reduce in the query also does not work, because valueChanges() never finishes.

Am I using the wrong operators? Or any other ideas on how to solve that?

I hope to hear from you.

Paul Coch
  • 11
  • 2

0 Answers0