简体   繁体   中英

Firebase .data() function is not found

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM