Kundan Agrawal
Kundan Agrawal

Reputation: 33

Chnges with regard to firebase v9

My friend wrote this code last year, Now that firebase v9 has some syntax changes, its not working anymore. What changes should i make -

useEffect(() => {
    const colRef = collection(db, 'movies',doc(id)) 
    colRef.doc(id)
    .get()
    .then((doc) => {
        if (doc.exists) {
            setDetailData(doc.data());
        } else {
            console.log("No such document in firebase 🔥");
        }
    })
    .catch((error) => {
        console.log("Error getting the document: ",error);
    });
}, [id]);

The problem is in the colRef.doc(id) section

Upvotes: 0

Views: 78

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

You'll find the solution in the documentation in the "Get a document" section (Tab "Web version 9").

import { doc, getDoc } from "firebase/firestore";


useEffect(() => {
    const docRef = doc(db, "movies", id);

    getDoc(docRef)
        .then((doc) => {
            if (doc.exists()) {
                setDetailData(doc.data());
            } else {
                console.log("No such document in firebase 🔥");
            }
        })
        .catch((error) => {
            console.log("Error getting the document: ", error);
        });
}, [id]);

Upvotes: 1

Related Questions