Firebase Firestore获取集合中每个文档的子集合

编程入门 行业动态 更新时间:2024-10-09 15:14:44
本文介绍了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获取集合中每个文档的子集合

本文发布于:2023-11-30 07:48:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   Firebase   Firestore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!