Phạm Cường
Phạm Cường

Reputation: 89

Cannot read properties of undefined reading 'deletePostById' Angular

TypeError: Cannot read properties of undefined (reading 'deletePostById')

What's the problem in my code?

HTML

<div class="row post" *ngFor="let post of posts">
     <div *ngIf="isAdminIn" ngbDropdown class="float-right" class=" col-md-1">
        <button (click)="deletePost(post.id)">Delete</button>
     </div>
</div>

.ts

 deletePost(id:number): void {
  this.postService.deletePostById(id).subscribe(data => {
    this.router.navigateByUrl('');
  }, error => {
    throwError(error);
  })

}

service.ts

 deletePostById(id: number): Observable<any> {
    return this.http.delete('http://localhost:8080/api/posts/' + id);
  }

Upvotes: 0

Views: 440

Answers (1)

AlleXyS
AlleXyS

Reputation: 2598

your postService is undefined. Have you injected it inside of component's constructor?

constructor(
  private postService: PostService
) {} 

??

Upvotes: 0

Related Questions