【问题标题】:how to get list of document in a collection, using angular firestore如何使用 Angular Firestore 获取集合中的文档列表
【发布时间】:2019-04-11 16:54:56
【问题描述】:

我正在尝试获取集合中的文档列表,但没有获取任何文档, 这是我正在尝试的代码- `

    schoolProductCollectionFetch(schoolName){
    
       const school = this.db.collection(schoolName)
       this.school$ = school.snapshotChanges().pipe(map(schoollist=>schoollist.map(s=>{
         const data = s.payload.doc.id;
         console.log(data);
         return data;
       })));
       return this.school$
     
      }

数据库格式: SchoolCollection->Documents->ProductCollection

SchoolCollection:{
   1st:{products:{}},
   2nd:{products:{}},
}

已尝试将数据格式表示为 firestore 中的数据格式,我想获得第一个,第二个,它们是 SchoolCollection 的文档 ID, 架构快照:

注意:如果需要更多详细信息来理解查询,请告诉我

【问题讨论】:

    标签: angular firebase google-cloud-firestore angularfire2


    【解决方案1】:

    你可以试试这样的:

    schoolProductCollectionFetch(): Observable<School[]> {
      return this.db
        .collection('schoolName')
        .snapshotChanges()
        .pipe(
          map((snaps) =>
            snaps.map((snap) => {
              return new School({
                id: snap.payload.doc.id,
                ...(snap.payload.doc.data() as {}),
              });
            }),
          ),
          first(),
        );
    }
    

    获取数据并创建实例后,您可以重构此代码,使其更具可读性。希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      我的第一个问题:您将参数“schoolName”传递给函数,然后执行this.db.collection(schoolName)。现在,学校名称实际上是集合的名称,还是集合中文档的 ID?

      当您尝试此操作时,您在控制台输出中遇到什么错误?您是否设置了 Firestore 权限以允许请求?

      您发布的数据库架构令人困惑,也许您从 firebase 控制台设置的实际屏幕截图可能更有用。

      编辑

      要使用 firestore 获取集合的数据,您必须构建指向所需集合的正确路径。在您的示例中,执行 this.db.collection(schoolName).get() 之类的操作将获取该集合中的所有文档。注意:这不会得到任何子集合!在撰写本文时,Firebase 不支持这种类型的查询。

      假设您想要获取子集合“kitProducts”的所有文档。这可以通过this.db.collection(schoolName + "/1st/kitProducts").get() 来实现。这将返回位于 schoolName 集合中 ID 为“1st”的文档下的子集合“kitProducts”中的所有文档。您必须建立这些路径才能查询它们。

      您似乎在收集实时听众的领域。如果这是您的意图,您应该使用onSnapshot() 函数。以前面的例子,写类似this.db.collection(schoolName + "/1st/kitProducts").onSnapshot() 的东西。在这种情况下,只要 kitProducts 子集合中的文档被编辑、删除或添加,就会发出一个事件。

      更多阅读:

      https://firebase.google.com/docs/firestore/query-data/listen#listen_to_multiple_documents_in_a_collection

      https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection

      【讨论】:

      • schoolName 参数包括集合的名称,我在控制台或运行该方法时没有收到任何错误,也没有得到任何输出,将使用架构屏幕截图修改问题
      • 使用架构快照更新问题,希望此详细信息将为解决方案查找添加一些输入
      • 感谢您的信息,但这并没有太大帮助,我已经浏览了此链接,但不知何故,在实施时我没有获得价值,如果您可以共享工作代码,那将更有帮助
      【解决方案3】:

      不确定你是否还需要这个,但你可能想试试collectionGroup('Name of collection')

      代码应该类似于以下内容:

          this.afs.collectionGroup('Users')
            .valueChanges().subscribe(users => {
            
            //this.userNames is an "Any" before my constructor.
            this.userNames = users
      
            //for loop allows me to get all the document's data
            for (let i = 0; i < users.length; i++) {
              
              //these const allow me to play with If conditionals
              const userEmail = this.userNames[i].email;
              const userRole = this.userNames[i].role;
      
              if (userRole == "User") {
                continue;
              } else {
                console.log(userEmail);
              }
            }
          });
      

      HTH 在任何东西...

      【讨论】:

        猜你喜欢
        • 2018-04-14
        • 2018-10-06
        • 1970-01-01
        • 2020-08-01
        • 2021-05-26
        • 2021-05-17
        • 2018-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多