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

z8dt9xmd  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(141)

我目前的react native/firebase的问题是在我的应用中查询我的论坛页面中每个帖子的评论。当用户进入论坛部分时,我的论坛数据库中的所有帖子都能正确加载,但当试图通过比较不同帖子的ID来获取每个帖子的评论时,react native不允许我这样做,并说:集合引用无效。集合引用必须具有奇数个段我如何修复此问题???

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()
  }, [])

火库结构:

xytpbqjk

xytpbqjk1#

要加载/查询特定论坛的评论,您必须在此处的路径中指定论坛ID:

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

如果要查询所有名为comments的集合,可以使用集合组查询,如下所示:

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

相关问题