Home > Mobile >  Firestore collection to array of objects with Firebase v9
Firestore collection to array of objects with Firebase v9

Time:09-07

I am creating a React Web Application. And I want to transform a firebase collection with documents into an array of objects. Every object in the array corresponding to a document in the collection. It would make my life easier. The reason why I'm asking is that i haven't been able to find a solution that works with Firebase Version 9.

CodePudding user response:

You can use getDocs() with your CollectionReference or a Query and then map an array from the QuerySnapshot received as shown below:

const q = collection(db, "users")
const querySnapshot = await getDocs(q) 

const arr = querySnapshot.docs.map((d) => ({ id: d.id, ...d.data() }))

Checkout the documentation for more information.

  • Related