Balázs Fodor-Pap
Balázs Fodor-Pap

Reputation: 608

Get all documents in collection using Cloud Firestore

I read several documentation but I don't understand why I should use an extra layer(foreach) in my code when I read all of the data inside a collection using Firebase (Cloud Firestore).

Here is the original documentation:
https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection

Here is my code:

  async loadUsers(): Promise<User[]> {
    const users = new Array<User>();
    const snapshot = await this.firestore.collection('users').get();
    snapshot.forEach((collection) => {
      collection.docs.forEach(doc => {
        users.push(doc.data() as User);
      });
    });
    return users;
  }

As I understand it should work like this:

async loadUsers(): Promise<User[]> {
    const users = new Array<User>();
    const snapshot = await this.firestore.collection('users').get();
    snapshot.forEach(doc => {
      users.push(doc.data() as User);
    });
    return users;
  }

Error message:
"Property 'data' does not exist on type 'QuerySnapshot'."

Upvotes: 0

Views: 2219

Answers (4)

Akshay K Nair
Akshay K Nair

Reputation: 1476

There seems to be no other way but to iterate.

  const q = query(collection(db, "item"));
  getDocs(q).then( response => {
    const result = response.docs.map(doc=>({ 
      id: doc.id, 
      ...doc.data(),
    }))
    console.log(result);
  }).catch(err=>console.log(err))

Upvotes: 0

Tarvo M&#228;esepp
Tarvo M&#228;esepp

Reputation: 4533

In new modular firebase firestore(version 9.+) it should be like this:

import { getFirestore, collection, query, getDocs } from 'firebase/firestore/lite'
async readAll() {
  const firestore = getFirestore()
  const collectionRef = collection(firestore, '/users')
  let q = query(collectionRef, orderBy('createTimestamp', 'desc'))
    
  const querySnapshot = await getDocs(q)
  const items = []
  querySnapshot.forEach(document => {
     items.push(document.data())
  })
  return items
}

I could not find any parameter on querySnapshot directly that is something like .docs was and included whole array before. So it is kinda like onSnapshot is and was.

Upvotes: 2

Bal&#225;zs Fodor-Pap
Bal&#225;zs Fodor-Pap

Reputation: 608

Based on @LeadDreamer answer, I could manage to simplify the code

  async loadUsers(): Promise<User[]> {
    const users = new Array<User>();
    await this.firestore.collection('users').get().subscribe(querySnapshot => {
      querySnapshot.docs.forEach(doc => {
        users.push(doc.data() as User);
      });
    });
    return users;
  }

Upvotes: 0

LeadDreamer
LeadDreamer

Reputation: 3499

.collection().get() does NOT return an array; it returns a QuerySnapshot, which has a property .docs, which is an array of QueryDocumentSnapshot, each of which has a property .data, which is the data read from the document.

Documentation https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference

Upvotes: 2

Related Questions