简体   繁体   English

关于 firebase v9 的变更

[英]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.我的朋友去年写了这段代码,现在 firebase v9 有一些语法变化,它不再起作用了。 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问题出在 colRef.doc(id) 部分

You'll find the solution in the documentation in the "Get a document" section (Tab "Web version 9").您将在“获取文档”部分(选项卡“Web 版本 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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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