AngularFire - 将两个 FireStore 集合与相同的 pushId 项目组合在一起 [英] AngularFire - combine two FireStore collections with the same pushId items
问题描述
我正在使用 AngularFire + FireStore 进行一些项目.
I'm working on some project with AngularFire + FireStore.
我的 Firestore 模型如下所示:
My Firestore model looks like this:
集合 1
Collection 1
- key1 {一些数据}
- key2 {一些数据}
第 2 集
- key1 {其他数据}
- key2 {其他数据}
现在我需要显示列表,其中有一些来自第一个集合的数据,以及一些来自另一个集合的数据.如何创建这样的列表?
Now I need to show list where I have some data from the first collection, and some data from another one. How to create such a list?
制作两个 observable 然后将其合并似乎毫无意义.
It seems to be pointless to make two observables and then merge it.
const collection = this.afStore.collection<any>(collectionName);
return collection.snapshotChanges()
.map(participants => {
return participants.map(participant => {
const data = participant.payload.doc.data();
const id = participant.payload.doc.id;
return {id, ...data};
});
});
我有这个需要数据 + id 的代码,现在我需要使用这个 id 从另一个集合中提取数据,但不知道如何.
I have this code that takes data + id, and now I need to use this id to pull data from another collection, but don't know how.
推荐答案
这是我的方法...
projects"数据有customer_doc_id、customer_builder_doc_id、customer_contractor_doc_id,它们是Firestore中其他数据集合的doc_id.命令 mergeMap 在 rxjs^5.5.0 中有效.
The "projects" data has customer_doc_id, customer_builder_doc_id, customer_contractor_doc_id, which are doc_id of other data collections in Firestore. The command mergeMap works in rxjs^5.5.0.
private getProjectsCombineObservables(): Observable<any[]> {
this.projectsCollection = this.afs.collection<Project>('projects', ref =>
ref.orderBy('job_number', 'desc'));
return this.projectsCollection.snapshotChanges()
.map(actions => {
return actions.map(a => {
const project_data = a.payload.doc.data() as Project;
const doc_id = a.payload.doc.id;
let observable1 = this.getCustomer(project_data.customer_doc_id);
let observable2 = this.getBuilder(project_data.customer_builder_doc_id);
let observable3 = this.getContractor(project_data.customer_contractor_doc_id);
const combinedData = Observable.combineLatest(observable1, observable2, observable3, (data1, data2, data3) => {
return { ...data1, ...data2, ...data3 };
});
return combinedData.map(data => Object.assign({}, { doc_id, ...project_data, ...data }));
});
}).mergeMap(observables => Observable.combineLatest(observables));
}
这篇关于AngularFire - 将两个 FireStore 集合与相同的 pushId 项目组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!