简体   繁体   English

按 ID 查询 firestore 中的子集合,版本 9

[英]Query subcollection in firestore by ID, version 9

The current issue with my current react native/firebase is querying the comments for each post in my forum page in my app.我当前的 react native/firebase 当前的问题是在我的应用程序的论坛页面中查询每个帖子的评论。 When a user goes to the forum section all the posts in my forums database load correctly but when trying to get the comments for each post by comparing the ID's of the different posts, react native does not allow me to do this and says: Invalid collection reference.当用户进入论坛部分时,我论坛数据库中的所有帖子都正确加载,但是当试图通过比较不同帖子的 ID 来获取每个帖子的评论时,本机反应不允许我这样做并说:无效的集合参考。 Collection references must have an odd number of segments how would i fix this???集合引用必须有奇数个段我将如何解决这个问题???

  useEffect(() => {
   async function ddd() {
    let todos = []
 // uid is already declared in my app and refers to the id of the post document
      try {
        const url = collection(db, `forums`,"comments");
    const q = query(url,where("uid","==",uid);
        const querySnapshot = await getDocs(q);
        querySnapshot.forEach((doc) => {
          // doc.data() is never undefined for query doc snapshots
          console.log(doc.data());
          todos.push(doc.data())
        });
        
      }
      catch(E){
        alert(E)
      }
      setData1(todos)
   }
   ddd()
  }, [])

firestore structure:防火结构: 在此处输入图像描述

To load/query the comments for a specific forum, you must specify the forum ID in the path here:要加载/查询特定论坛的评论,您必须在此处的路径中指定论坛 ID:

const url = collection(db, "forums", "the forum ID such as R3SXOkxj...", "comments");

If you want to query across all collections named comments , you can use a collection group query , which would look like:如果你想查询所有 collections 命名的comments ,你可以使用集合组查询,它看起来像:

const url = collectionGroup(db, "comments");

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

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