Skip to content
Advertisement

Firestore: get an array of objects

Is it possible to get an array of objects from Firestore. I tried something like below but I am getting undefined when I tried to log comments[0].comment

JavaScript

Advertisement

Answer

This returns a QuerySnapshot which contains the DocumentSnapshot of each document that has matched your query.

JavaScript

The array of object is a field in your document. You cannot get a single field from a document. You need to fetch the document and then access that field hence you make that query above first.

Now commentsSnapshot.docs is an array of DocumentSnapshots. Now if you know there is only one matching document you can access it’s data like this:

JavaScript

In case your QuerySnapshot has multiple documents, you can loop thought the docs as it is an array.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement