Home > Net >  .get() function to get firebase docs "is not a function"
.get() function to get firebase docs "is not a function"

Time:06-19

Hey I am trying to get all documents of a certain collection in firebase using:

const userCollectionRef = collection(db, currentUser?.uid)
const snapshot = await userCollectionRef.get()
for (const doc of snapshot) {
    console.log(doc.id)
}

The problem:

TypeError: userCollectionRef.get is not a function

My imports:

import { db } from '../firebase'
import { collection } from 'firebase/firestore'

CodePudding user response:

There's a top-level function getDocs() that you can use to fetch multiple documents using a CollectionReference in V9 SDK:

import { collection, getDocs } from "firebase/firestore";

const userCollectionRef = collection(db, currentUser?.uid)

const snapshot = await getDocs(userCollectionRef)

const data = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() }))

Checkout the documentation for more information.

  • Related