所以我遇到了一种情况,我试图从firestore数据库中的多个不同表中提取数据。我用firebase实时数据库做过这种事情,但是我认为我在这里做错了什么,或者我遗漏了什么。
我有一个函数,它运行多个层次
combineLatest
和
switchMap
函数获取数据并将其返回给函数的原始调用方,如下所示:
/**
* Get the groupings and then get the mapping data for each
*/
getGroupingsData(userId, businessAccountId, duration) {
// User's access token
let at = localStorage.getItem('access_token');
return Observable.combineLatest(
Observable.fromPromise(this.getPagesForUser()),
this.getGroupingsForUserAndAccount(userId, businessAccountId)
).switchMap(results => {
let fbPages = results[0].data;
let groupings = results[1];
// Let's iterate through each mapping
return Observable.combineLatest(
groupings.map(grouping => {
let group = grouping.payload.doc.data();
return Observable.combineLatest(
group.mappings.map(mapping => {
return this.getMappingData(mapping.id).switchMap(data => {
let mapData = data.payload.data();
let pageAccessToken = '';
// Loop through pages and see if one can find a matching fb ID
fbPages.forEach(page => {
// We have a match, add the page access token to the mapping
if(page.id === mapData.page.id) {
pageAccessToken = page.access_token;
}
});
// Get the data for the duration
return Observable.combineLatest(
this.getFacebookAdAccountStats(mapData.ad_account.id),
Observable.fromPromise(this.getInsightsForPage(mapData.page.id, pageAccessToken, duration))
)
.switchMap(theData => {
let adAccountStatsData = theData[0];
let fbPageInsightsData = theData[1];
let mappingAdData = {
ad_account: {
insights: {}
},
page: {}
}
mappingAdData['ad_account']['insights'] = adAccountStatsData;
mappingAdData['page']['insights'] = {
reach_total_paid: fbPageInsightsData[0].data[0].values[1].value,
reach_total_organic: fbPageInsightsData[1].data[0].values[1].value,
impressions_total_paid: fbPageInsightsData[2].data[0].values[1].value,
impressions_total_organic: fbPageInsightsData[3].data[0].values[1].value,
engaged_users_total: fbPageInsightsData[4].data[0].values[1].value,
};
return Observable.of(mappingAdData);
})
})
})
)
})
)
})
}
在这个特定的例子中,来自
combineLatest()
函数正确地冒泡,但是在它到达
.subscribe()
我在另一个组件中。
有什么方法可以从最深层的数据中提取数据,并将其添加到包含函数中更高层数据的对象中,然后返回到调用函数?我意识到这是一个有点复杂的查询,我试图拼凑在一起,但有什么可以做的吗?