Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 561
Cyberguy Game Avatar asked May 01 '26 04:05

Cyberguy Game


1 Answers

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.

like image 67
Dharmaraj Avatar answered May 02 '26 19:05

Dharmaraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!