简体   繁体   中英

What is the typeof of a QuerySnapshot?

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? eg 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?

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 (eg @firebase/firestore )?

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.

https://github.com/firebase/firebase-js-sdk/blob/cdada6c68f9740d13dd6674bcb658e28e68253b6/packages/firestore/src/api/snapshot.ts#L432-L452

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM