我有一项服务,我无法从forkJoin
返回正确的数据。
public loadMatchAnalysis(clientId: string): void {
this.matchAnalysisService
.getClientPolicySummary(clientId)
.pipe(
switchMap(clientPolicySummary => {
const mappings$ = flatMap(clientPolicySummary.policyGroups, policyGroup => {
return policyGroup.policies.map(policy => this.matchAnalysisService.getMappings(clientId, policy.id));
});
const calculations$ = flatMap(clientPolicySummary.policyGroups, policyGroup => {
return policyGroup.policies.map(policy =>
this.matchAnalysisService.getCalculationMethods(clientId, policy.id)
);
});
return forkJoin(of(clientPolicySummary), ...mappings$, ...calculations$); //can't get this to return properly
})
)
.subscribe(
([clientPolicySummary, mappings, calculations]: [
ClientPolicySummary,
MatchAnalysisMapping[],
Calculation[]
]) => {
let matchAnalysis: MatchAnalysis[] = [];
console.log('mappings', mappings);
console.log('calculations', calculations); //this will be what 'mappings' should be
clientPolicySummary.policyGroups.forEach(policyGroup => {
policyGroup.policies.forEach(policy => {
const _mappings = mappings.filter(m => m.id === policy.id);
const _calculations = calculations.filter(c => c.id === policy.id);
matchAnalysis.push({ policy: policy, mappings: _mappings, children: _calculations });
});
});
new GetMatchAnalysisLoadedAction({ data: matchAnalysis }).dispatch();
},
error => {
}
);
}
基于我的forkJoin
的输入,我试图返回数据作为subscribe()
中参数的元组。如果我在mappings$
或calculations$
上使用散布运算符,则返回的数据将是使用该运算符的先到者。这让我觉得我不应该在这里使用散布运算符-但是我不确定要执行其他操作来返回数据并将其作为不可观察的数据进行访问。
答案 0 :(得分:0)
forkJoin是一个rxjs运算符,所有这些运算符都处理异步数据流。因此,您必须返回一个Observable,该Observable是异步数据的包装器。
对于返回元组,整个元组必须是可观察的。但是switchmap会将值包装在一个新的可观察值中,因此不必要。
您可以返回此值并将其键入为tuple
[clientPolicySummary, mappings$, calculations$]
或者如果您遇到类型错误,则将其包装在可观察的范围内
of([clientPolicySummary, mappings$, calculations$])
答案 1 :(得分:0)
诀窍在于:
return forkJoin(of(clientPolicySummary), forkJoin(...mappings$), forkJoin(...calculations$));
因为否则就什么也没有将映射和计算收集到它们自己的数组中。