我有一个方法将它作为表单管理的一部分,并执行下面的操作。我可以很容易地用承诺来解决这种情况,但是这里是学习观察者的时间!因此,该方法的任务:当flatMaps没有流通过时,应该调用什么方法而不是在rxjs中进行订阅?
- 电话
clientWebApiService.getClients()
拿到客户名单,并返回观察到的environmentWebApiService.getEnvironmentsNotInPipeline()
第二flatMap
- 结果将被传递到本地变量在
subscribe
- 我想映射的数据结构的另一个,以便能够一形式
的subscribe()
的设置东西抛出下方的错误:
ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at Object.subscribeToResult (subscribeToResult.js:73)
at MergeMapSubscriber._innerSub (mergeMap.js:130)
at MergeMapSubscriber._tryNext (mergeMap.js:127)
我已经(希望)想通了,订阅期望一个流,这里没有提供。如果我看到正确的第二个flatMap应该返回一个由subscribe()
处理的Observable。在这个结构中不会有。在这种情况下应该调用什么方法?我发现rxjs documentation有很多方法。我不知道我在找哪一个。
我试图重构我的代码,但如果没有subscribe()
,那么就没有执行。
- 什么是这样的最佳实践案例?
public editHandler({ dataItem }): void {
let clients: IClientContract[];
let environments: IEnvironmentContract[];
this.clientWebApiService.getClients()
.flatMap((clientsResult: any): Observable<IEnvironmentContract[]> => {
clients = clientsResult as IClientContract[];
console.log('1', clients);
return this.environmentWebApiService.getEnvironmentsNotInPipeline();
})
.flatMap((environmentResult: IEnvironmentContract[]): any => {
environments = environmentResult;
console.log('2', environments);
})
.subscribe(() => {
console.log('3');
let editableDeploymentItem: IDeployableEnvironmentForm = this.deployableEnvironmentContractMapperService
.mapDeployableEnvironmentContractToDeployableEnvironmentForm(
dataItem,
clients,
environments);
console.log('4', editableDeploymentItem);
this.editDeployment = editableDeploymentItem;
this.isNew = false;
});
}