Skip to content

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

Answer

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