6

I cant seem to get my delete request to work. I have finished all of the get requests but now I'm stuck on delete and can't seem to wrap my head around it.

The console.log'd URL is always correct and the delete request works fine via Postman.

Got any ideas?

HTML

<button class="button button3" (click)="delTicket()"><span class="fa fa-trash"></span></button>

TS

delTicket(){
    this.id = this.route.snapshot.params.id;
    this.ticketService.deleteTicket(this.id);
}

Service

deleteTicket(id): Observable<Ticket[]>{
    console.log(this.apiUrl + id);
    return this.http.delete<Ticket[]>(this.apiUrl + id);
}
3
  • 1
    Any errors in the console? Commented Sep 9, 2018 at 12:46
  • None, just logs the URL. Commented Sep 9, 2018 at 12:46
  • what abt network tab in dev tool? are u able to del it after the API ia called and page is refreshed? ? Commented Sep 9, 2018 at 12:46

2 Answers 2

21

You need to call subscribe() inside your component, otherwise request wont get invoked

delTicket(){
    this.id = this.route.snapshot.params.id;
    this.ticketService.deleteTicket(this.id).subscribe((data)=>{
         console.log("success");
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

In 3 minutes :D
5

You must call subscribe() or nothing happens. Just calling ticketService.deleteTicket(this.id) does not initiate the DELETE request.

  1. An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. This is true for all HttpClient methods.
  2. Calling subscribe(...) triggers execution of the observable and causes HttpClient to compose and send the HTTP request to the server.

2 Comments

Dude this was september 2018 :D
@HeikoPiirme Doesn't matter . addtional explaination is always good :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.