Reputation: 341
There is a api post method that is failing sometimes.If it is failing i am showing error message to the page. But sometimes when there is error on the api, it is still getting in the next method and then i have errors. I want to be sure when there is not error,so on the complete method to initialise my property on the response that is coming. My code looks like this.
this.attService.postUser(user).subscribe((response: any) => {
this.postRequestResponse = response;
this.selectedServiceSpeedsArr.push(this.selectedServiceSpeed);
let index = this.selectedAttService[0].serviceSpeeds.findIndex(item => item.id == event.id);
}, (err) => {
this.makeQuoteErrorMessage = 'Something went wrong.Please try again';
}, () => {
console.log("success");
//can i done something like this ?
myProperty = response;
})
}
Upvotes: 1
Views: 43
Reputation: 403
Yes you can, as you see here :
export class MyComponent implements OnInit {
constructor(private http: HttpClient) {}
ngOnInit() {
let result = this.http.get(<YOUR_URL>);
result.subscribe(
res => {
// this is your response with no errors
},
err => {
// here you handle your errors
},
() => {
// here is the finally block where you can do things either you got errors or not
}
);
}
}
You may wanna give this one a shot, good luck!
Upvotes: 1