Firebase Firestore获取集合中每个文档的子集合 [英] Firebase Firestore Get Subcollections for each document in a collection

查看:64
本文介绍了Firebase Firestore获取集合中每个文档的子集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库有一个部分的集合,每个部分都包含一个子部分的子集合.

My database has a collection of sections, each of which contain a subcollection of subsections.

这是我需要创建的数组:

Here's the array I need to create:

[
  {
    "sectiontitle": "first section",
    "sectiontype": "1",
    "__id__": "6jwwukHmOs1743yC13vH",
    "subsections": [
      {
        "desc": "blah blah blah",
        "__id__": "KTq4MJTEMbDfR33RoX9J"
      },
      {
        "desc": "blah blah blah",
        "__id__": "ASFSADFSAFSADFSADFFS"
      }
    ]
  },
  {
    "sectiontitle": "second section",
    "sectiontype": "1",
    "__id__": "5jwwDHEDOs1743yC13vG",
    "subsections": [
      {
        "desc": "blah blah blah",
        "__id__": "DFG4MJTEMbDfR33DDDDD"
      },
      {
        "desc": "blah blah blah",
        "__id__": "ASDFGHFSAFSADFSHJFRD"
      }
    ]
  }
]

我的代码是:

    // get sections
    firebase.firestore().collection("sections").get().then((sections) => {
      var myarray = [];
      var i = 0;
      sections.forEach((section) => {
        var sectiondata = section.data();
        sectiondata.__id__ = section.id
        sectiondata.subsections = [];
        myarray.push(sectiondata);

        //get subsections
        firebase.firestore().collection("sections").doc(sectiondata.__id__).collection("subsections").get().then((subsections) => {
          subsections.forEach((subsection) => {
            var subsectiondata = subsection.data();
            subsectiondata.__id__ = subsection.id;
            myarray.push('sections.' + i + '.subsections', subsectiondata); // problem is i index is not correct
          });
        });

        i++;

      });
    });

此问题是 i 索引不正确,因为读取的部分已经完成,并且在读取子部分开始时已遍历foreach.我不熟悉Javascript承诺,所以我希望有人可以帮助我解决该问题?

This problem is that the i index is not correct as the sections read has already finished and looped through the foreach by the time the subsections read has started. I'm not familiar with Javascript promises so I was hoping someone can help me out with how to solve this problem?

基本上,对于每个部分,请添加子部分.

Basically, for each section, add the subsections.

谢谢.

推荐答案

请尝试一下,让我知道是否正常工作.请注意,SubjectBehavior是从RxJs库导入的,而我的代码是打字稿代码,因为我认为您使用的是打字稿.

Pls try this and let me know if worked correctly or not. Notice that SubjectBehavior is imported from RxJs library, and my code is a typescript code, as i think u r using typescript.

   sb = new SubjectBehavior<[]>();
    var basicArray = [];
           firebase.firestore().collection("sections").get().then((sections) => {
              var myarray = [];
              basicArray = [];
              var count = 0;

              sections.forEach((section) => {
                var sectiondata = section.data();
                sectiondata.__id__ = section.id
                sectiondata.subsections = [];
                myarray.push(sectiondata);
            count++;
            if( count === sections.length()) {
                sb.next(myarray); //or .emit(), i dont remember
            }
                //get subsections

            });

        sb.subscribe( (myArrayElements: any[]) => {
            myArrayElement.foreach( (element) => {
    firebase.firestore().collection("sections").doc(element.__id__).collection("subsections").get().then((subsections) => {
                      subsections.forEach((subsection) => {
                        var subsectiondata = subsection.data();
                        subsectiondata.__id__ = subsection.id;
                        basicArray.push('here element data (sectionData)' + '' + '.subsections', subsectiondata); 
                      });
                    });

                  });
    });
    });

这篇关于Firebase Firestore获取集合中每个文档的子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆