0
votes

I want to ask my firestore for an array of documents which are holding information referencing to other documents.

get "user" document => returns ids of "items" documents => foreach(get "item" document)

getUserOwnedItems(uid:string):Observable<Item[]>{
    return this.firestore
          .collection<any>(`members`, ref => ref.where('uid', '==', uid))
          .valueChanges()
          .pipe(
            switchMap(ids => { // these ids are representing the "item" ids 
              return ids.map(id => {
                return this.firestore
                  .doc<Item>(`items/${id.itemId}`)
                  .valueChanges();
              });
            })
            ,concatAll() // <-- Iam not sure about this
          );
}

The problem is Iam expecting an observable with an Array of Items. Instead I get a observable of Item back.

How can I convert this to

Observable<Item[]>

? Thanks in advance!

My Structure in firestore looks as follows:

members : [
 {
  uid:string,
  itemId:string,
  ...
 }
]

items : [
 {
  id:string,
  ...
 }
]

The Use Case in other words: I want to get all Items related to a specific user identified by its uid.

Update: I found out that all concats mergs or forkJoins are not working in this scenario because the firebase observables will never be completed.

I could do something like ...

return this.firestore
   .doc<Item>(`items/${id.itemId}`)
   .valueChanges().pipe(first());

But this will break my realtime connectivity. Besides this every call of this function will result in reads on FireStore which will cost money.

1
Why are you using concatAll ? - user4676340
the idea is to combine all the requests and send them back as one Observable. - Danny
SwitchMap returns a single result, so you actually have a single result. - user4676340

1 Answers

0
votes

You probably wanted to use combineAll instead of concatAll if you need an Observable<Item[]>.

combineAll

Flattens an Observable-of-Observables by applying combineLatest when the Observable-of-Observables completes.

concatAll

Converts a higher-order Observable into a first-order Observable by concatenating the inner Observables in order