Angular 财产';地图';不存在于类型';可观察<;文档更改操作<;客户>;[]>';
我是angular的一个完全初学者,我目前正在学习一些课程,我对firestore不太了解,我正在做一个项目,通过这个项目我应该了解firebase,所以请假装我是白痴或5岁的孩子,或者用正确的代码回答。多谢各位Angular 财产';地图';不存在于类型';可观察<;文档更改操作<;客户>;[]>';,angular,google-cloud-firestore,Angular,Google Cloud Firestore,我是angular的一个完全初学者,我目前正在学习一些课程,我对firestore不太了解,我正在做一个项目,通过这个项目我应该了解firebase,所以请假装我是白痴或5岁的孩子,或者用正确的代码回答。多谢各位 export class ClientService { clientsCollection: AngularFirestoreCollection<Client>; clientDoc: AngularFirestoreDocument<Client>
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;
}
导出类ClientService{
客户集合:AngularFirestoreCollection;
clientDoc:AngularFirestoreDocument;
客户:可观察;
客户:可观察;
构造函数(专用afs:AngularFirestore){
this.clientscolection=this.afs.collection('clients',ref=>ref.orderBy('lastName','asc');
}
getClients():可观察{
//获取具有id的客户端
this.clients=this.clientsCollection.snapshotChanges().map(更改=>{
返回更改。映射(操作=>{
const data=action.payload.doc.data()作为客户端;
data.id=action.payload.doc.id;
返回数据;
});
});
把这个还给我的客户;
}
您需要在可观察对象的管道下使用map
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;
});
})
);
使用管道
接收(
地图(…),
开关映射(…)
)
this.shirtCollection=afs.collection('shirts');
//.snapshotChanges()返回DocumentChangeAction[],其中包含
//关于每次更改“发生了什么”的大量信息。如果你想
//使用map操作符获取数据和id。
this.shirts=this.shirtCollection.snapshotChanges().pipe(
map(actions=>actions.map(a=>{
const data=a.payload.doc.data(),如图所示;
const id=a.payload.doc.id;
返回{id,…data};
}))
);
如前所述
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 };
}))
);