【问题标题】:How to find the parent collection to a document in Firebase Firestore?如何在 Firebase Firestore 中找到文档的父集合?
【发布时间】:2019-10-05 05:47:05
【问题描述】:

我正在 Firebase 中搜索子查询,并试图找到它所属的父集合(然后是父文档)。这是我的代码

let queryRef = orgRef.where('User_ID', '==', userID).get()
    .then(snapshot => {
        if (snapshot.empty) {
          console.log('No matching documents.');
          return;
        }  



        snapshot.forEach(doc => {
          console.log(doc.parent());
        });
      })
      .catch(err => {
        console.log('Error getting documents', err);
      });

但是使用 doc.parent 或 snapshot.parent 不起作用。如何找到子集合的父文档?

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    如果你有一个 DocumentSnapshot 对象,你会想要使用它的ref 属性来获取一个指向它的DocumentReference 对象。 DocumentReference 有一个parent 属性,它将为您提供文档所在的集合。所以,你可能会想要:

    doc.ref.parent
    

    对于查询结果中的任何给定doc

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2018-10-21
      • 2019-06-14
      • 2022-01-05
      • 2019-11-04
      • 2018-05-10
      • 2021-02-20
      • 2021-03-18
      • 2021-01-23
      相关资源
      最近更新 更多