Map items of collection snapshot in Firebase Firestore

João Souza picture João Souza · Oct 6, 2017 · Viewed 23.6k times · Source

Firebase Firestore Guides show how to iterate documents in a collection snapshot with forEach:

db.collection("cities").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
});

I imagined it would support map as well, but it doesn't. How can I map the snapshot?

Answer

João Souza picture João Souza · Oct 6, 2017

The answer is:

querySnapshot.docs.map(function(doc) {
  # do something
})

The Reference page for Firestore reveals the docs property on the snapshot.

docs non-null Array of non-null firebase.firestore.DocumentSnapshot

An array of all the documents in the QuerySnapshot.