首页 文章

当没有流通过时,应该在flatMaps之后调用rxjs中的什么方法而不是订阅?

提问于
浏览
1

我有一个方法将其作为表单管理的一部分并执行以下操作 . 我可以轻松地用承诺来解决这样的情况,但是现在是时候学习观察了!那么,方法的任务:

  • 调用 clientWebApiService.getClients() 获取客户列表,并返回 environmentWebApiService.getEnvironmentsNotInPipeline() 的observable

  • 在第二个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)

我已经(希望)发现subscribe需要一个这里没有提供的流 . 如果我看到正确的话,第二个flatMap应该返回一个由 subscribe() 处理的Observable . 在这种结构中将没有 . 在这种情况下应该调用什么方法?我找到了很多方法的rxjs documentation . 我不想找到.1535012_m .

我试图重构我的代码,但如果没有 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;
            });
    }

2 回答

  • -1

    你应该使用 do . do 操作员只是执行一个动作并继续蒸汽 . 在你的情况下,我不明白为什么你需要另一个运算符,因为它可以在 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();
                })
                .subscribe((environmentResult: IEnvironmentContract[]): any => {
                    environments = environmentResult;
                    console.log('2', environments);
                    console.log('3');
                    let editableDeploymentItem: IDeployableEnvironmentForm = this.deployableEnvironmentContractMapperService
                        .mapDeployableEnvironmentContractToDeployableEnvironmentForm(
                        dataItem,
                        clients,
                        environments);
                    console.log('4', editableDeploymentItem);
    
                    this.editDeployment = editableDeploymentItem;
                    this.isNew = false;
                });
        }
    
  • 1

    而是使用 this.clientWebApiService.getClients().flatMap(() => {}) 你可以尝试使用 this.clientWebApiService.getClients().map(() => {}) 吗?

    阅读this可能有所帮助

相关问题