NgRx: get '您提供了 'undefined' 预期流的位置。有效调用完整操作时
NgRx: get 'You provided 'undefined' where a stream was expected.' when calling complete action in effect
我正在尝试执行 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)
条件分支中没有任何内容 return。据我了解,您想调用两个 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 转换为可从 rxjs
导入的 Observable,或者您可以将 combinedFunction
修改为 return 一个 Observable。
希望这对您有所帮助:)
我正在尝试执行 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)
条件分支中没有任何内容 return。据我了解,您想调用两个 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 转换为可从 rxjs
导入的 Observable,或者您可以将 combinedFunction
修改为 return 一个 Observable。
希望这对您有所帮助:)