尝试获取api的响应时,可观察到的抛出未处理的错误。 api发送状态为200的响应时,代码不会引发错误。
但是只要不这样做,控制台上就会出现一个巨大的错误,来自常绿区。
代码:
component.ts
this.appService.signinFunction(data).subscribe((apiResponse) => {
if (apiResponse.status === 200) {
// this.router.navigate([]);
this.toastr.success('hurray!!');
console.log(apiResponse);
this.cookie.set('authToken', apiResponse.data.authToken);
this.cookie.set('receiverId', apiResponse.data.userDetails.userId);
this.cookie.set('receiverName', apiResponse.data.userDetails.firstName + ' ' + apiResponse.data.userDetails.lastName);
this.appService.setUserInfoInLocalStorage(apiResponse.data.userDetails);
this.router.navigate(['/chat']);
} else if (apiResponse.status === 400){
console.log("hello");
// this.toastr.error(apiResponse.message);
}
},
(err) => {
// console.log("helo")
// this.toastr.error(err);
});
服务
public signinFunction(data):Observable<any>{
const params=new HttpParams()
.set('email', data.email)
.set('password',data.password);
return this.http.post(`${this.url}/users/login`, params);
}
尝试邮递员时api响应消息:
{
"error": true,
"message": "Wrong Password.Login Failed",
"status": 400,
"data": null
}
但是在角度上会给出区域错误:
POST https://chatapi.com/api/v1/users/login 400
scheduleTask @ zone-evergreen.js:2952
scheduleTask @ zone-evergreen.js:378
onScheduleTask @ zone-evergreen.js:272
scheduleTask @ zone-evergreen.js:372
scheduleTask @ zone-evergreen.js:211
scheduleMacroTask @ zone-evergreen.js:234
scheduleMacroTaskWithCurrentZone @ zone-evergreen.js:1107
(anonymous) @ zone-evergreen.js:2985
proto.<computed> @ zone-evergreen.js:1428
(anonymous) @ http.js:2065
_trySubscribe @ Observable.js:42
subscribe @ Observable.js:28
(anonymous) @ subscribeTo.js:20
subscribeToResult @ subscribeToResult.js:7
_innerSub @ mergeMap.js:59
_tryNext @ mergeMap.js:53
_next @ mergeMap.js:36
next @ Subscriber.js:49
(anonymous) @ scalar.js:4
_trySubscribe @ Observable.js:42
subscribe @ Observable.js:28
call @ mergeMap.js:21
subscribe @ Observable.js:23
call @ filter.js:13
subscribe @ Observable.js:23
call @ map.js:16
subscribe @ Observable.js:23
LoginComponent.signinFunction @ login.component.ts:37
(anonymous) @ LoginComponent.html:48
handleEvent @ core.js:38092
callWithDebugContext @ core.js:39710
debugHandleEvent @ core.js:39346
dispatchEvent @ core.js:25806
(anonymous) @ core.js:37024
(anonymous) @ platform-browser.js:1789
invokeTask @ zone-evergreen.js:391
onInvokeTask @ core.js:34176
invokeTask @ zone-evergreen.js:390
runTask @ zone-evergreen.js:168
invokeTask @ zone-evergreen.js:465
invokeTask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629