0
votes

I am using firestore

I have health name collection

collection health
doc customername
collection customerinfo  // multiple
doc customerwardinfo

Code:

firebase.firestore()
        .collection("health")
        .doc('customername')
        .get()
        .then((data)=>{            
            console.log("customer data",data);
        })

i am using the above code but not getting data. i want to use for each for some sub collections. but i am not able to implement it. how i will get all data. suppose a collection has 10 docs. then use foreach for each doc and get data from each doc.

1
If you're asking how to get a list of all subcollections of a specific document, that is not possible with the client-side SDKs. See stackoverflow.com/questions/46596532/… - Frank van Puffelen

1 Answers

0
votes

.doc().get() does NOT return a promise of the Document; it returns a promise of a DocumentSnapshot, which contains a great deal MORE than just the document. https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot

As you can see, DocumentSnapshot has properties "exists", "metadata", "id" and "ref", and methods "data()" [which returns an object with the document fields - the data], "get()" [which can return specified fields from the document], and "isEqual()", which can compare DocumentSnapshots.

on .doc().get().get() - while the final result would be only the requested fields, the entire document is fetched from the database/store.

As Frank van Puffelen mentions, there is no way to retrieve a list of sub-collections - Firestore does NOT enforce or document your schema; that's your job as a developer.

Tracy Hall