Do something after a DELETE request is complete with Angular HttpClient

Simeon Nakov

I have a button which deletes an item on click. I want to show a simple alert('Item deleted') alert after the action is done. However I can't seem to make it work. Note that the item gets deleted.

Here's what I tried:

deleteItem function in my component

deleteItem(id: number) {
    this.eventsService
      .deleteEventById(id)
      .subscribe(
        res => console.log(res),
        err => console.log(err),
        () => alert('Item deleted')
      );
  }

Nothing is printed to the console and no alert is shown.

deleteEventById function in my service

deleteEventById(id: number) {
  return this.http
    .delete(`/api/events/${id}`, this.headerForPrivateCalls)
    .pipe(
      catchError(this.handleError),
    );
}

api route (if it matters)

router.delete('/events/:id', authCheck, (req, res) => {
    connection((client) => {
        client.db('gordes').collection('events')
        .deleteOne({id: +req.params.id})
        .then(() => {
            res.status(204);
        })
        .catch((err) => {
            sendError(err, res);
        });
    })
});
Aniruddha Gohad

your deleteEventById service does not return anything to the Observable. You will need to add a .map()

    deleteEventById(id: number) {
       return (this.http
        .delete(`/api/events/${id}`, this.headerForPrivateCalls)
        .map(response => response.json())
        .pipe(
           catchError(this.handleError),
        );
      )
   }

EDIT

the problem seems to be in the node back-end :

you dont return anything by

res.status(204);

use :

res.status(204).send();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

nodejs do something after multiple functions complete

Angular - When observable is complete, do something

Angular4: HttpClient delete request with body

Angular delete button in HttpClient request with Json Server

How do I "do something" after delete with Silverstripe

Angular HttpClient Delete

Angular HttpClient - request not executed

Unable to do post request with HttpClient in angular using proxy config with solution

Angular 2 HttpClient Delete collection

Angular 4 how to wait for httpclient complete

How to handle RxJs timeout complete - Angular HttpClient

Buffering an angular service HttpClient request

issue with httpclient request Array in Angular

Complete request on error in Angular Interceptor

After change URL do something

Do something after rendering in mojolicious

Do something after returning a response?

Angular 2: How to do something in a while loop every 5 seconds after EVERYTHING else is finished in angular 2?

How do you make a request with matrix parameters in the URI to a backend when using Angular's HttpClient?

Auto complete filter function only works after type something

What does delete[something] do in visual studio?

change readyState to 0 after request complete in javascript

Creating list items after GET request is complete

Calling api request after Complete other requests

do something at unsuccessful jquery ajax request

Angular HttpClient request with debounceTime does not wait for dueTime

Angular HttpClient request method how to set body

Angular 5 - httpClient is not sending headers on request

Angular HTTP Request Works, but HttpClient Fails