Firestore: Get subcollection of document found with where

£可爱£侵袭症+ 提交于 2020-01-23 17:55:08

问题


I am trying to get the documents inside an subcollection which is part of an document found with the .where function

Exemple:

  • RootColl/
    • Doc A/
      • SubColl 1
        • Doc 1
        • Doc 2
        • Doc 3
      • SubColl 2
        • Docs
    • Doc A/
      • SubColl 1
        • Doc 1
        • Doc 2
        • Doc 3
      • SubColl 2
        • Docs

I want to get all the documents under SubColl 1 from the doc with the field level == 1

I am trying to do it like:

db.collection("RootColl").where("field", "==", "1").collection("SubColl 1").get()

But by doing that i get the error

Uncaught TypeError: db.collection(...).where(...).collection is not a function

EDIT 1: By following Frank van Puffelen suggestion, i get the same error, "collection" is not a function

  • Current Code
  • Error

回答1:


A sub-collection lives under a specific document. A query as you've shared now points to a number of documents. You'll need to execute the query to determine what documents it points to, then loop over the results, and get the sub-collection for each document.

In code:

var query = db.collection("RootColl").where("field", "==", "1");
query.get().then((querySnapshot) => {
  querySnapshot.forEach((document) => {
    document.ref.collection("SubColl 1").get().then((querySnapshot) => {
      ...
    });
  });
});


来源:https://stackoverflow.com/questions/47634907/firestore-get-subcollection-of-document-found-with-where

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!