用于集合的Firestore方法snapshotChanges()

时间:2017-10-25 04:53:27

标签: google-cloud-firestore angularfire5

以下是Collections in AngularFirestore中提供的代码。

export class AppComponent {
  private shirtCollection: AngularFirestoreCollection<Shirt>;
  shirts: Observable<ShirtId[]>;
  constructor(private readonly afs: AngularFirestore) {
    this.shirtCollection = afs.collection<Shirt>('shirts');
    // .snapshotChanges() returns a DocumentChangeAction[], which contains
    // a lot of information about "what happened" with each change. If you want to
    // get the data and the id use the map operator.
    this.shirts = this.shirtCollection.snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data() as Shirt;
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    });
  }
}

此方法snapshotChanges()返回DocumentChangeAction []的observable。那么为什么当它只有一个数组并且只循环一次时使用地图来读取它呢?

0 个答案:

没有答案