Sharvari Desai Sharvari Desai - 1 month ago 7
Javascript Question

Basic Javascript filter function not working correctly in Angular2

This should be incredibly simple, but my filter function in Typescript keeps giving me an error. Here is my code:

highScores: HighScore[];

deletePlayer(email: string) {
this.scoreDataService.deletePlayer(email)
.subscribe(
this.highScores = this.highScores.filter(highScore => highScore.email !== email)
);
}


The filter function should simply return an array HighScore[], but instead, I keep getting this error:

Argument of type 'HighScore[] is not assignable to parameter of type 'NextObserver<Response> | ErrorObserver<Response> | CompletionObserver<Response> ...

Type 'HighScore[]' is not assignable to type '(value: Response) => void'. Tyope 'HighScore[]' provides no match for the signature '(value:Response): void'


The weirdest thing is that this code runs and works perfectly even with this error. Does anyone know what could be happening? Thank you in advance!

Answer

subscribe expects a function argument:

this.scoreDataService.deletePlayer(email) 
  .subscribe(() => {
     this.highScores = this.highScores.filter(highScore => highScore.email !== email)
   });
Comments