【发布时间】:2019-10-02 18:28:49
【问题描述】:
我有一个由 firestore 返回的对象填充的屏幕,但我无法在同一个 onInit 上返回两个查询的数据
服务:
export class TcpService implements MPersist{
tcpsO: Observable<TcpID[]>;
private tcpCollection: AngularFirestoreCollection<Tcp>;
constructor(private db: AngularFirestore) { }
addDoc(data: Tcp) {
this.tcpCollection.add({ nome: data.nome, nivel: data.nivel, registro: data.registro })
}
editDoc(data:TcpID){
this.tcpCollection.doc(data.id).set({ nome: data.nome, nivel: data.nivel, registro: data.registro })
}
removeDoc(id){
this.tcpCollection.doc(id).delete();
}
getDocs() {
this.tcpCollection = this.db.collection<Tcp>('tcps');
return this.tcpsO = this.tcpCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Tcp;
const id = a.payload.doc.id;
return { id, ...data }
}))
);
}
}
export interface TcpID extends Tcp { id: string }
对于组件:
ngOnInit() {
return this.service.getDocs().subscribe(
res => {
this.dataSource.sort = this.sort;
this.dataSource.data = res;
}
);
}
所以我可以操作数据,但是当我尝试返回 2 个不同的查询不起作用时,我需要用来自 2 个不同查询的信息填充组件
【问题讨论】:
-
您可以使用 combineLatest 或 forkJoin 可观察运算符,具体取决于您的用例
标签: angular typescript google-cloud-firestore observable