Firebase .data() function is not found

Viewed 25

I did manually set up the entire data into Firebase Firestore have been trying to retrieve the data as followed

import app, { db } from "./app";
import {
    addDoc,
    collection,
    doc,
    getDocs,
    getFirestore,
    limit,
    orderBy,
    query,
    setDoc,
    Timestamp,
} from "firebase/firestore";


export default async function getEvents() {
    const db = getFirestore(app);
    const first = collection(db, "Events");
    const docSnap = await getDocs(first);
    console.log(docSnap.data());
}

The thing over here is that the console throws an error saying that docSnap.data() is not a function

TypeError: docSnap.data is not a function

1 Answers

Yes apparently the mistake I did was that I need to specify the document before I get it's data, So the correct thing to do would be

console.log(docSnap.docs[0].data());

If you want all the data from all the elements maybe you can loop over them or just map them depending over your preference.

Related