我是一名完全有经验的初学者,我正在学习一些课程,而我对Firestore的了解不多,我正在做一个项目,通过这个项目,我应该学习Firebase,所以请假装我是白痴还是5岁的孩子,或只回答正确的代码。谢谢。
export class ClientService {
clientsCollection: AngularFirestoreCollection<Client>;
clientDoc: AngularFirestoreDocument<Client>;
clients: Observable<Client[]>;
client: Observable<Client>;
constructor(private afs: AngularFirestore) {
this.clientsCollection = this.afs.collection('clients', ref => ref.orderBy('lastName', 'asc'));
}
getClients(): Observable<Client[]> {
// Get clients with the id
this.clients = this.clientsCollection.snapshotChanges().map(changes => {
return changes.map(action => {
const data = action.payload.doc.data() as Client;
data.id = action.payload.doc.id;
return data;
});
});
return this.clients;
}
答案 0 :(得分:1)
您需要在Observable的map
下使用pipe
。
import { map } from 'rxjs/operators'
this.clients = this.clientsCollection.snapshotChanges().
.pipe(
map(changes => {
return changes.map(action => {
const data = action.payload.doc.data() as Client;
data.id = action.payload.doc.id;
return data;
});
})
);
答案 1 :(得分:0)
使用管道 rx。( 地图(...), switchMap(...) )
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().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Shirt;
const id = a.payload.doc.id;
return { id, ...data };
}))
);
此处https://github.com/angular/angularfire/blob/master/docs/firestore/collections.md