Trajce12
Trajce12

Reputation: 341

How ti initialise some property to the response when the subscribe method is successfully done - in the complete method?

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

Answers (1)

Daasrattale
Daasrattale

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

Related Questions