【问题标题】:how to get data firebase collection in collection如何在集合中获取数据 Firebase 集合
【发布时间】:2021-06-01 03:21:45
【问题描述】:

我想在集合'action'中获取数据

ticket
  ->document in ticket collection(id)
    ->**action**
      ->document in action collection(id) 
         -> data

我有 .ts 和服务,但我不知道如何在 .html 中显示数据

app.html

<div class="row" *ngFor="let action of Action">
   *<!-- {{ action.payload.doc }} -->
   <!-- {{ getAction(action) }} -->*
</div>

app.ts

  ngOnInit() {
    this.auth.user$.subscribe(user => this.user = user)
    this.ticket$ = this.ticketService.getTicketByid(this.id);
    this.action = this.ticketService.getTrack(this.id).snapshotChanges().subscribe(data => {
      data.map(items => {
        const item = items.payload.doc.data();
        const id = items.payload.doc.id;
        return { id, ...item };
      })
    })
  }

应用程序服务

  getTrack(id: string): any {
    return this.afs.collection('ticket').doc(id)
      .collection('action', ref => ref
        .orderBy('date', 'desc'))
  }

【问题讨论】:

  • 至少在*ngFor="let action of Action"&gt;this.action = this.ticketService.getTrack(this.id).snapshotChanges()... 之间的大小写不匹配。我建议将集合命名为复数,所以 this.actions = this.ticketService.getTrack(this.id).snapshotChanges()*ngFor="let action of actions"&gt;

标签: javascript angular firebase google-cloud-firestore angularfire2


【解决方案1】:

请使用以下代码从 firestore 获取子数据。

getTrack(id: string){
    let docRef = this.fireservices.collection('ticket');
    const tickets = docRef.snapshotChanges().pipe(
       map((actions) =>
         actions.map((a) => {
           //this.parentId is your parent documentId
           this.parentId = a.payload.doc.id;
           return docRef.doc(id).collection('action').valueChanges();
         })
       )
     );
     tickets.subscribe(data => {
       data[0].subscribe(child => {
         //Here you can get the child data
         console.log(child[0]);
       });
     });
}

【讨论】:

  • 这是您的服务 getTrack 功能
  • 我已经在代码中注释了你可以获取子数据的地方
  • 您可以从服务中返回 const 票证变量,并且可以在您的 component.ts 中订阅。
  • 我的服务 getTrack 功能不起作用?
  • 您是在问将采集的数据放入采集中对吗?
猜你喜欢
  • 2021-07-23
  • 2021-07-25
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 2022-01-15
  • 2022-11-16
相关资源
最近更新 更多