Reputation: 193
I am trying to obtain the data of a document, and also this document has a field that is a collection. How could I get all that data? my document has the following properties:
I use angularFire package.
name: string, lastName: string, items: Collection.
This document is from a collection users:
Mi code: collection('users').doc(ID).collection('items').snapshotChanges().subscribe(val =>console.log('val',val), but this return [{type:{},payload:{}},{},....]
I tried changing snapshotChange to ValueChanges, but that only brings me data from my internal collection (items)
Upvotes: 2
Views: 721
Reputation: 369
As of now, there is no built-in way of fetching the data of the document and its subcollections in a single query.
One solution would be to make multiple queries, one for fetching the user and another one for fetching his items. Here is one way of doing it using AngularFire:
const userDoc = collection('users').doc(ID);
const userItems = userDoc.collection('items').get();
const user = userDoc.get();
Now both user
and userItems
are Observables of Snapshot.
Depending on what you store in the items
subcollection, you might want to store items
as an array on the users
collection. This way you'll be able to fetch it in a single query on users
.
Upvotes: 0