我正在尝试执行 API 调用并将现有数据与现有数据合并(合并是在 addMissingData 中完成的)。
当我尝试调用时出现错误:
Uncaught TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at subscribeTo (subscribeTo.js:27) at subscribeToResult (subscribeToResult.js:11) at MergeMapSubscriber._innerSub (mergeMap.js:59) at MergeMapSubscriber._tryNext (mergeMap.js:53) at MergeMapSubscriber._next (mergeMap.js:36) at MergeMapSubscriber.next (Subscriber.js:49) at MapSubscriber._next (map.js:35) at MapSubscriber.next (Subscriber.js:49) at CatchSubscriber._next (Subscriber.js:72) at 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)
条件分支。据我了解,您想调用两个 APIgetCustomerMasterData
getCustomerMasterData
的结果,您要调用addMissingData
我的建议是创建另一个函数来调用上面的 apis,然后使用管道。
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 转换为可以从 rxjs
导入的 Observable ,或者您可以修改 combinedFunction
返回一个 Observable。希望这有帮助:)
关于angular - NgRx: 得到 'You provided ' undefined' where a stream is expected.'当调用有效的完整 Action 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63409991/