I see a lot of tutorials doing something like this:
http.get("...").subscribe(
success => console.log('hello success'),
error => console.log('bye error')
);
I don't know how this works, since there aren't any types or anything, however I tried to do that myself and I end up that the request always goes into success, even if I have an error. What is the problem?
Troublemaker:
this.memberService.create(this.currentMember)
.subscribe(
success => {
let mem: Member = success.json() as Member;
if (this.selectedOrganization) {
this.addMemberToOrganization(mem);
} else if (this.selectedServiceProvider) {
this.addMemberToServiceProvider(mem);
} else {
this.toastsService.error("lbl_users_service_provider_and_organization_undefined");
}
},
error => console.log(error)
);
Create-Method in the memberService:
create(member: Member): Observable<any> {
return this.http
.post(this.RESOURCE_BASE_URL, member)
.map(response => {
if (response.status === 200) this.toastsSerivce.success(this.translateService.instant('lbl_users_member_created'));
return response;
})
.catch(error => this.toastsSerivce.error(this.translateService.instant('lbl_users_member_create_failed')));
}
I even catch the error, but the subscribe
part doesn't seem to care.
It fails at success.json()
, because if there is an error, there is no json
. But if there is an error, I want it to call the error =>...
instead of the success
. Any advice is highly appreciated.