NgRx:获取“您在期望流的位置提供了“未定义”调用有效的完整动作时

问题描述

我正在尝试进行API调用,并将现有数据与现有数据合并(合并在addMissingData中完成)。 尝试致电:

时出现错误

未捕获的TypeError:您提供了流未定义的位置的“未定义” 预期。您可以提供一个Observable,Promise,Array或Iterable。

atsubscribeto(subscribeto.js:27) 在subscriptionToResult(subscribetoResult.js:11) 在MergeMapSubscriber._innerSub(mergeMap.js:59) 在MergeMapSubscriber._tryNext(mergeMap.js:53) 在MergeMapSubscriber._next(mergeMap.js:36) 在MergeMapSubscriber.next(Subscriber.js:49) 在MapSubscriber._next(map.js:35) 在MapSubscriber.next(Subscriber.js:49) 在CatchSubscriber._next(Subscriber.js:72) 在CatchSubscriber.next(Subscriber.js:49)

 GetStepWiseMasterData$ = createEffect(() => this.actions$.pipe(
            ofType(MasterDataAction.getCustomerMasterData),switchMap((action) => {
                return this.dataService.GetDataAsync(action.stepEnum).pipe(
                    map((response) => {
                        return {
                            isSuccess: response.isSuccess,newData: response.data,data: action.data,};
                    }),mergeMap((response) => {
                        if (response.isSuccess) {
                            this.addMissingData(response.newData,action.data).then(result => {
                                return [DataAction.getCustomerDataComplete({ customerData: result })];
                            });
                        } else {
                            return [DataAction.getCustomerDataFailed({ customerData: {} })];
                        }
                    }),catchError(() => EMPTY)
                );
            })
        ));

   async addMissingData(newMasterData: BusinessPartnerMasterDataVM,existingMasterData: BusinessPartnerMasterDataVM = {}): Promise<any> {
        const omitNullValues = masterDataObj => new Promise((resolve) => {
            // filter with property name and delete all properties that are null
            Object.keys(masterDataObj).filter(propertyName => masterDataObj[propertyName] === null).forEach(propertyName => delete(masterDataObj[propertyName]));
            resolve(masterDataObj);
        });
        const masterNew = await omitNullValues(newMasterData);
        const masterOld = await omitNullValues(existingMasterData);
        const ret = Object.assign({},masterNew,masterOld);
        return ret;
    }

解决方法

您的问题似乎发生在该区域,

mergeMap((response) => {
    if (response.isSuccess) {
        this.addMissingData(response.newData,action.data).then(result => {
            return [DataAction.getCustomerDataComplete({ customerData: result })];
        });
    } else {
        return [DataAction.getCustomerDataFailed({ customerData: {} })];
    }
})

在这里,if (response.isSuccess)条件分支中没有任何返回。据我了解,您想调用两个API

  1. 首次通话getCustomerMasterData
  2. 然后使用getCustomerMasterData的结果来调用addMissingData

我建议创建另一个函数以在api之上调用,然后使用管道。

GetStepWiseMasterData$ = createEffect(() => this.actions$.pipe(
    ofType(MasterDataAction.getCustomerMasterData),switchMap((action) => {
        return from(this.combinedFunction(action)).pipe(
            map((result) => {
                return { masterData: result };
            }),switchMap((result) => {
                return [MasterDataAction.getCustomerDataComplete(result)];
            })
        );
    })
));

async combinedFunction(action) {
    const response = await this.dataService.GetDataAsync(action.stepEnum).toPromise();
    if (!response.isSuccess) {
        return {};
    }
    return await this.addMissingData(response.newData,action.data);
}

from是将Promise转换为Observable的必需条件,可以从rxjs导入,也可以修改combinedFunction以返回Observable。

希望这会有所帮助:)