Google cloud firestore 集合的Firestore方法snapshotChanges()

Google cloud firestore 集合的Firestore方法snapshotChanges(),google-cloud-firestore,angularfire5,Google Cloud Firestore,Angularfire5,以下是中提供的代码 导出类AppComponent{ 私人衬衫系列:AngularFirestoreCollection; 衬衫:可见; 构造函数(专用只读afs:AngularFirestore){ this.shirtCollection=afs.collection(“衬衫”); //.snapshotChanges()返回DocumentChangeAction[],其中包含 //关于每次更改“发生了什么”的大量信息。如果你想 //使用map操作符获取数据和id。 this.shirts

以下是中提供的代码

导出类AppComponent{
私人衬衫系列:AngularFirestoreCollection;
衬衫:可见;
构造函数(专用只读afs:AngularFirestore){
this.shirtCollection=afs.collection(“衬衫”);
//.snapshotChanges()返回DocumentChangeAction[],其中包含
//关于每次更改“发生了什么”的大量信息。如果你想
//使用map操作符获取数据和id。
this.shirts=this.shirtCollection.snapshotChanges().map(操作=>{
返回actions.map(a=>{
const data=a.payload.doc.data(),如图所示;
const id=a.payload.doc.id;
返回{id,…data};
});
});
}
}
这里,方法snapshotChanges()返回DocumentChangeAction[]的可观察值。那么,当一个映射只有一个数组并且只循环一次时,为什么要使用它来读取它呢

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 };
      });
    });
  }
}