【发布时间】:2018-11-18 18:18:58
【问题描述】:
我正在使用 combineLatest 合并来自 Firestore 的 3 个不同查询。但是,我不想使用 valueChanges(),我想使用 snapshotChanges()。
const newRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'new')).snapshotChanges().pipe(
map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as Application;
const id = a.payload.doc.id;
return {id, ...data};
})
})
);
const pendingRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'pending')).snapshotChanges().pipe(
map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as Application;
const id = a.payload.doc.id;
return {id, ...data};
})
})
);
const inprogressRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'in-progress')).snapshotChanges().pipe(
map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as Application;
const id = a.payload.doc.id;
return {id, ...data};
})
})
);
const result = combineLatest<any[]>(newRef, pendingRef, inprogressRef).pipe(
map(arr => arr.reduce((acc, cur) => acc.concat(cur)))
);
return result;
如何合并这 3 个查询以获取它们各自的文档 ID? 我必须以这种方式编写 3 个查询还是有其他方式?我想简化代码。
【问题讨论】:
-
你现在有什么问题?
-
实际上一切正常,但我想以一种不必复制 snapshotChanges() 代码块的方式编写代码。
标签: javascript firebase rxjs google-cloud-firestore angularfire2