NGRX returns 在抛出错误之前成功
NGRX returns success before throwing error
我在我的服务中抛出一个异常,并试图在我的效果中捕获异常,return 在前端正确的吐司。
发生了什么:看起来我收到一条成功消息,然后抛出一个错误,我在控制台中看到了错误,但从未在前端看到“错误”吐司.我在服务的 catcherror 行上放置了一个断点。在命中断点之前我看到了成功祝酒词。
应该发生什么:捕获错误后应该在前端显示“错误”toast。
有人能看出我做错了什么吗?
服务
saveBusiness(business: BusinessModel) {
return from(this.businessCollection.add(JSON.parse(JSON.stringify(business)))).pipe(
catchError(err => throwError(err))
);
}
效果
AddBusiness$ = createEffect(() =>
this.actions$.pipe(
ofType(BusinessActions.AddBusiness),
mergeMap(({ business }) =>
of(this.businessService.saveBusiness(business)).pipe(
map(() => BusinessActions.CreateBusinessSuccess()),
catchError(() => of(BusinessActions.CreateBusinessFailure()))
)
)
)
);
动作
export const CreateBusinessFailure = createAction(
'[Business] Failed Creating to database'
);
组件代码段
actions$
.pipe(ofType(BusinessActions.CreateBusinessSuccess))
.subscribe((data) => {
this.notifyService.showSuccess(
'Business was saved successfully',
'Success'
);
});
actions$
.pipe(ofType(BusinessActions.CreateBusinessFailure))
.subscribe((data) => {
this.notifyService.showError(
'Business was not saved. Please check the information and try again',
'Failed creating business'
);
});
不需要在服务中捕获错误只是为了再次抛出它:
saveBusiness(business: BusinessModel) {
return from(this.businessCollection.add(JSON.parse(JSON.stringify(business)))).pipe(
catchError(err => throwError(err))
);
}
效果会捕获错误(假设是“add”或JSON.parse抛出错误:
saveBusiness(business: BusinessModel) {
return from(this.businessCollection.add(JSON.parse(JSON.stringify(business))));
}
您不需要在效果中创建另一个可观察对象 - 去掉“of()”,但请注意这里的 catchError 现在有错误:
AddBusiness$ = createEffect(() =>
this.actions$.pipe(
ofType(BusinessActions.AddBusiness),
mergeMap(({ business }) => this.businessService.saveBusiness(business).pipe(
map(() => BusinessActions.CreateBusinessSuccess()),
catchError(( error ) => of(BusinessActions.CreateBusinessFailure( error )))
)
)
)
);
我在我的服务中抛出一个异常,并试图在我的效果中捕获异常,return 在前端正确的吐司。
发生了什么:看起来我收到一条成功消息,然后抛出一个错误,我在控制台中看到了错误,但从未在前端看到“错误”吐司.我在服务的 catcherror 行上放置了一个断点。在命中断点之前我看到了成功祝酒词。
应该发生什么:捕获错误后应该在前端显示“错误”toast。
有人能看出我做错了什么吗?
服务
saveBusiness(business: BusinessModel) {
return from(this.businessCollection.add(JSON.parse(JSON.stringify(business)))).pipe(
catchError(err => throwError(err))
);
}
效果
AddBusiness$ = createEffect(() =>
this.actions$.pipe(
ofType(BusinessActions.AddBusiness),
mergeMap(({ business }) =>
of(this.businessService.saveBusiness(business)).pipe(
map(() => BusinessActions.CreateBusinessSuccess()),
catchError(() => of(BusinessActions.CreateBusinessFailure()))
)
)
)
);
动作
export const CreateBusinessFailure = createAction(
'[Business] Failed Creating to database'
);
组件代码段
actions$
.pipe(ofType(BusinessActions.CreateBusinessSuccess))
.subscribe((data) => {
this.notifyService.showSuccess(
'Business was saved successfully',
'Success'
);
});
actions$
.pipe(ofType(BusinessActions.CreateBusinessFailure))
.subscribe((data) => {
this.notifyService.showError(
'Business was not saved. Please check the information and try again',
'Failed creating business'
);
});
不需要在服务中捕获错误只是为了再次抛出它:
saveBusiness(business: BusinessModel) { return from(this.businessCollection.add(JSON.parse(JSON.stringify(business)))).pipe( catchError(err => throwError(err)) ); }
效果会捕获错误(假设是“add”或JSON.parse抛出错误:
saveBusiness(business: BusinessModel) {
return from(this.businessCollection.add(JSON.parse(JSON.stringify(business))));
}
您不需要在效果中创建另一个可观察对象 - 去掉“of()”,但请注意这里的 catchError 现在有错误:
AddBusiness$ = createEffect(() =>
this.actions$.pipe(
ofType(BusinessActions.AddBusiness),
mergeMap(({ business }) => this.businessService.saveBusiness(business).pipe(
map(() => BusinessActions.CreateBusinessSuccess()),
catchError(( error ) => of(BusinessActions.CreateBusinessFailure( error )))
)
)
)
);