如何解决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
- 首次通话
getCustomerMasterData
- 然后使用
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。
希望这会有所帮助:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。