简体   繁体   English

如何使用模块 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合?

[英]How do I get document and nested collection in a document in a Cloud Firestore collection using Version 9 of the Modular Web SDK?

I am following along with a tutorial in which using Firestore to retrieve data from a collection.我正在学习使用 Firestore 从集合中检索数据的教程。 there two major things: retrieve DOCUMENT and COLLECTION that's nested inside that DOCUMENT.有两件主要的事情:检索 DOCUMENT 和嵌套在该 DOCUMENT 中的 COLLECTION。 My code down below is based on FireStore web v8, and I want to convert it to web version 9. I found it very complicated !我下面的代码是基于 FireStore web v8,我想把它转换成 web 版本 9。我发现它很复杂!

    useEffect(()=>{
        if(roomId){
            db.collection('rooms').doc(roomId).onSnapshot(snapshot => {
                setRoomName(snapshot.data().name);
            });

            db.collection('rooms').doc(roomId).collection("messages").orderBy("timestamp","asc").onSnapshot(snapshot => {
                setMessages(snapshot.docs.map(doc => doc.data()))
            });

        }
    },[roomId])

Try this:尝试这个:

import { collection, query, where, doc, onSnapshot } from "firebase/firestore";

useEffect(() => {
  onSnapshot(doc(db, "rooms", roomId), (doc) => {
    setRoomName(doc.data().name)
  });

  const q = query(collection(db, "rooms", roomId, "messages"), orderBy("timestamps"));

  onSnapshot(q, (querySnapshot) => {
    setMessages(querySnapshot.docs.map(doc => doc.data()))
  });
}, [roomId])

Also checkout:还结帐:

This one should do the job !这个应该做的工作!

    useEffect(() => {
        if(roomId){
            onSnapshot(doc(db, "rooms", roomId), (document) =>{ 
                setRoomName(document.data().name);
            });
            const msgColl = query(collection(db, "rooms", roomId, "messages"), orderBy("timestamp"));
            onSnapshot(msgColl, (querySnapshot) => {
                setMessages(querySnapshot.docs.map(msg => msg.data()))
            });
        }
    }, [roomId])

This is just for others....这只是为了其他人......

V8: V8:

export const fetchAccountAssets = (userID, accountID) => db()
  .collection('Accounts')
  .doc(userID)
  .collection('Assets')
  .get()
  .then((querySnapshot) => querySnapshot);

V9: V9:

export const fetchAccountAssets = async (userID, accountID) => {
  return await getDocs(collection(db, 'Accounts', userID, 'Assets'));
}

I dont think I need to await here but oh well..我不认为我需要在这里等待,但是哦,好吧..

暂无
暂无

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

相关问题 如何使用模块 Web SDK 的版本 9 获取 Cloud Firestore 集合中的所有文档? - How do I get all documents in a Cloud Firestore collection using Version 9 of the Modular Web SDK? 如何获取集合 Cloud Firestore 中的文档 ID 列表? - How to get a list of document IDs in a collection Cloud Firestore? 如何检查 JS 中的 firestore(不是文档)中是否存在集合 - How do I check if collection exist in firestore (not document) in JS 如何使用angular firestore获取集合中的文档列表 - how to get list of document in a collection, using angular firestore 未创建文档的 Cloud Firestore 子集合 - Cloud Firestore Sub-Collection on a Document not Creating Firestore - 如何在将文档添加到集合后获取文档 ID - Firestore - How to get document id after adding a document to a collection Firebase / Firestore 将文档添加到子集合版本 9 - Firebase / Firestore Add Document to Sub collection Version 9 使用云功能 typescript 在 Firestore 中创建子子集合时如何更新子集合文档 - How to update a sub-collection document when sub sub-collection is created in firestore using cloud functions typescript 如何使用 Flutter 检索我的 Cloud Firestore 集合中最后添加的文档? - How to retrieve the last added document in my Cloud Firestore collection, using Flutter? 如何从 Firebase Firestore 集合中指定文档的特定字段获取数据并将其显示到我的 UI? - How do I get data from a specific field of the specified document inside a Firebase Firestore collection and display it to my UI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM