如何获取多个文档和子集合Firestore

时间:2019-06-07 07:23:15

标签: angular google-cloud-firestore angular7 angularfire2

正在工作的有角度的消防站项目,想要创建以下结果:

   [{
      label: 'Item 1.2',
      queries: [
        {
          label: 'Item 1.2.1',
        },
        {
          label: 'Item 1.2.2',
        }
    }]

enter image description here

尝试:

//获取收集数据

this.blockCollection = this.afs.collection<Block>
  ('blocks', ref => ref.orderBy('name', 'asc'));
this.blocks = this.blockCollection.snapshotChanges().pipe(
  map(actions => actions.map(a => {
    const data = a.payload.doc.data() as Block;
    data.id = a.payload.doc.id;
    return data;
  }))
);


    const that = this;
    this.blocks.subscribe({
      next(x) {
        that.showList(x);
      },
      error(err) {
        this.msgType = 'danger';
        this.msg = 'Error! while getting records.';
      }
    }); 

 // Creating list
  private showList(list) {
    this.block_list = [];
    list.forEach(t => {
      const item = {
        label: t.name,
      };
      if (t.queries) {
        const sub = [];
        t.queries.forEach(q => {
          const sitem = {
            label: q.name,
          };
          sub.push(item);
        });
        item['items'] = sub;
      }
      this.block_list.push(item);
    });
  }

0 个答案:

没有答案