1

I have trouble with subcribe method. In vscode it says that subcribe is deprecated but I have no clue how to change it properly.

  public getAccount(): void{
    this.accountService.getAccounts().subscribe(
      (response: Account[]) => {
        this.accounts = response;
      },
      (error: HttpErrorResponse) => {
        alert(error.message);
      }
    )
  }
Tsvetan Ganev
  • 8,246
  • 4
  • 26
  • 43
Aitsuken
  • 126
  • 10
  • there was a discussion going on in github issue regarding the same. So it is probably because of vscode update and ESlint something. Visit this link https://github.com/microsoft/TypeScript/issues/43053 – Wahab Shah May 11 '22 at 13:27
  • Does this answer your question? [Subscribe is deprecated: Use an observer instead of an error callback](https://stackoverflow.com/questions/55472124/subscribe-is-deprecated-use-an-observer-instead-of-an-error-callback) – JSON Derulo May 11 '22 at 13:32

1 Answers1

4

You should pass an observer object instead of multiple callbacks. All signatures that used multiple arguments were deprecated.

this.accountService.getAccounts().subscribe({
  next: (response: Account[]) => {
    this.accounts = response;
  },
  error: (error: HttpErrorResponse) => {
    alert(error.message);
  },
  complete: () => {
    // do something when the observable completes
  }
});

If you don't need an error and complete callbacks, you can still use it like this: .subscribe((value) => console.log(value)).

You can read about why the signature you're using was deprecated here.

Tsvetan Ganev
  • 8,246
  • 4
  • 26
  • 43