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.
concatAll? - user4676340