【问题标题】:Angular Firestore Collection Snapshot ChangesAngular Firestore 集合快照更改
【发布时间】:2019-08-13 17:26:45
【问题描述】:

我正在使用 FireStore,我只需要将集合列表数据显示到组件上,并且在显示的每个项目旁边都可以删除该项目。

我需要使用 SnapShotChanges() 来获取我的删除方法的 ID。

这是我的代码。没有控制台被控制台记录。我正在使用 Angular 7。

  userCollection: AngularFirestoreCollection<any>;
  collection: any;

  constructor(private afs: AngularFirestore, private adminService: AdminServiceService) { }

  ngOnInit() {
    this.userCollection = this.afs.collection<any>('valuation');
    this.collection = this.userCollection.snapshotChanges().pipe(
      map(actions => {
        actions.map(a => {
          console.log(a.payload.doc.data);
          console.log(a.payload.doc.id);
        })
      }
    ))
  }

这是我的模板:

<tbody *ngFor="let o of collection | async">
      <tr>
        <td scope="row">
          {{o.address}}
        </td>
        <td scope="row">
          {{o.name}}
        </td>
        <td scope="row">
          {{o.email}}
        </td>
        <td scope="row">
          {{o.phone}}
        </td>
        <td scope="row">
          <button mat-button color="warn" class="delete" type="button" (click)="delete(o)">Delete</button>
        </td>
      </tr>
    </tbody>

【问题讨论】:

  • 开发者工具 > 网络选项卡上有什么有趣的地方吗?
  • 很遗憾没有...
  • 您尝试 console.log 的方式有点奇怪,因为它是返回对象的一部分。您可以尝试在地图运算符之前添加一个点击运算符吗? tap( (actions) =&gt; console.log('actions', actions));

标签: angular firebase google-cloud-firestore


【解决方案1】:

当您使用snapshotChanges 时,AngularFirestore 会为您提供文档以及文档的id

然后您可以在action 上使用payload.doc.data() 提取文档的实际值。

试试这个:

import { Component } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  userCollection: AngularFirestoreCollection<any>;
  collection: any;

  constructor(private afs: AngularFirestore) { }

  ngOnInit() {
    this.userCollection = this.afs.collection<any>('valuation');
    this.collection = this.userCollection.snapshotChanges()
      .pipe(
        map(actions => actions.map(a => a.payload.doc.data()))
      );
  }
}

这里有一个Working Sample StackBlitz 供您参考。

【讨论】:

  • 我现在可以在控制台中正确打印 a.payload.doc.data() 了。但它拒绝在模板中显示而没有错误。
  • ngOnInit() { this.userCollection = this.afs.collection('valuation'); this.collection = this.userCollection.snapshotChanges().pipe( map(a => { a.map(item => { return item.payload.doc.data(); }) }) )
  • 这 : ngOnInit() { this.userCollection = this.afs.collection('valuation'); this.collection = this.userCollection.snapshotChanges().pipe( map(a =&gt; { a.map(item =&gt; { return item.payload.doc.data(); }) }) ) 应该是 ngOnInit() { this.userCollection = this.afs.collection('valuation'); this.collection = this.userCollection.snapshotChanges().pipe( map(a =&gt; { return a.map(item =&gt; { return item.payload.doc.data(); }) }) ) 要么去掉 { } 要么为 a.map(...) 放置一个 return 部分
  • 如果您不订阅 DocumentChangeAction 观察者,这应该如何工作?
猜你喜欢
  • 2020-06-09
  • 2018-03-18
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2018-08-22
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多