【问题标题】:Is there a way to get all documents in a subcollection (Firestore)有没有办法获取子集合中的所有文档(Firestore)
【发布时间】:2020-02-27 14:17:58
【问题描述】:

我在查询 Firestore 数据库的子集合中的所有文档时遇到了困难。 (我正在使用 Node.js)

我的目标是从名为 favorites 的子集合中的每个文档中获取所有数据

我的数据库结构如下所示。

我查看了https://firebase.google.com/docs/firestore/query-data/get-data 的文档,但没有任何结果如何解决问题。

我的查询现在看起来像这样:

exports.getAllFavoritePodcasts = (req, res) => {
  db.collection('users')
    .doc(req.user.userId)
    .collection('favorites')
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
      });
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });
}

但我得到TypeError: db.collection(...).doc(...).collection(...).then is not a function

【问题讨论】:

  • 我不熟悉firebase,但你不是在then之前错过了.get() -> db.collection(...).doc(...).collection(...).get().then
  • 这解决了我的问题。非常感谢@Molda :)

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


【解决方案1】:

总而言之,必须调用 get() 方法来检索结果。在您提到的 Cloud Firebase 文章中,也可以在 example 获取集合中的所有文档中找到它。

【讨论】:

    【解决方案2】:

    使用 Firebase 版本 9(2021 年 9 月更新):

    如果 subcollection(subcoll) 包含 3 个文档(doc1, doc2, doc3),如下所示:

    coll > doc > subcoll > doc1 > field1: "value1", field2: "value2"
                           doc2 > 字段 1:“值 1”,字段 2:“值 2”
                           doc3 > 字段 1:“v1”,字段 2:“v2”
    

    您可以使用以下代码获取子集合(subcoll)的所有3个文档(doc1,doc2,doc3):

    import { getDocs, collection } from "firebase/firestore";
    
    const docsSnap = await getDocs(collection(db,"coll/doc/subcoll"));
          
    docsSnap.forEach((doc) => {
      console.log(doc.data()); // "doc1", "doc2" and "doc3"
    });
    

    没有forEach()方法获取子集合(subcoll)的全部3个文档(doc1, doc2, doc3):

    import { getDocs, collection } from "firebase/firestore";
    
    const docsSnap = await getDocs(collection(db, "coll/doc/subcoll"));
            
    console.log(docsSnap.docs[0].data()); // "doc1"
    console.log(docsSnap.docs[1].data()); // "doc2"
    console.log(docsSnap.docs[2].data()); // "doc3"
    

    另外,你可以通过下面的while得到2个文档(doc1, doc2)的子集合(subcoll):

    import { query, collection, where, getDocs } from "firebase/firestore";
    
    const q = query(collection(db, "coll/doc/subcoll"), 
                    where("field1", "==", "value1"));
        
    const docsSnap = await getDocs(q);
    
    docsSnap.forEach((doc) => {
      console.log(doc.data()); // "doc1" and "doc2"
    });
    

    如果没有forEach()方法获取2个文档(doc1, doc2)的子集合(subcoll),while如下:

    import { query, collection, where, getDocs } from "firebase/firestore";
    
    const q = query(collection(db, "coll/doc/subcoll"),
                    where("field1", "==", "value1"));
        
    const docsSnap = await getDocs(q);
        
    console.log(docsSnap.docs[0].data()); // "doc1"
    console.log(docsSnap.docs[1].data()); // "doc2"
    

    同样,另外,您可以仅使用以下代码获取子集合(subcoll)的一个特定文档(doc3):

    import { getDoc, doc } from "firebase/firestore";
    
    const docSnap = await getDoc(doc(db, "coll/doc/subcoll/doc3"));
              
    console.log(docSnap.data()); // "doc3"
    

    【讨论】:

      猜你喜欢
      • 2020-01-22
      • 1970-01-01
      • 2018-04-03
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多