With firebase, you can get documents in a collection like this: const collectionData = await collectionPath.get();
using collectionData, you can loop with:
collectionData.forEach(doc=>{ // doc here is a document, and we can get data with doc.data()});
The above code makes collectionData behave like an array of documents. However, you can still treat it like an object by calling things like size? e.g collectionData.size
will return the size of items in there.
My question is, what data type is a QuerySnapshot or the data returned by calling .get() on a firebase reference?
CodePudding user response:
Per the documentation, QuerySnapshot has the docs
field which represents an array of QueryDocumentSnapshot<T>>
so you could call QuerySnapshot.docs.length
to see the size of the collection.
For the collectionData questions, could you post more of your example code? Which module is this using (e.g. @firebase/firestore
)?
CodePudding user response:
The QuerySnapshot
is a regular JS object with a few custom methods in it, and one of them is the forEach
method that essentially works as a proxy, kind of shortcut, to the actual docs.forEach
array method.