TheUnreal
TheUnreal

Reputation: 24472

RxJS & Firestore - map list of document ids to the documents

I stored in my database a list of IDS of posts the user has liked, now I want to show the list of posts he liked.

I'm trying to map the IDS to the posts using RXJS.

So far, I got the list of the ids in a snapshot, and i'm not sure how to switch them to the documents themselves:

myLikedConfessions() {
    return this.db.doc(this.USER_COLLECTION)
      .collection('likedConfs',
          ref => ref.limit(5))
      .snapshotChanges()
      .mergeMap((ids) => {
        console.log(ids);
 // each id should have the observable result of: this.db.collection('posts').doc(docID);

          return Observable.of({});
      })
  }

Any idea?

Upvotes: 1

Views: 262

Answers (1)

martin
martin

Reputation: 96891

Just use forkJoin that waits until all source Observables complete:

...
.mergeMap((ids) => {
  const observables = ids.map(id => this.db.collection('posts').doc(id))
  return Observable.forkJoin(observables)
})

Upvotes: 2

Related Questions