Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 - HTTP delete not working

Tags:

angular

I am trying to create a MEAN crud operation. I have an api in node with the delete http method as so localhost:3000/api/user?id=<some document id>. Below is the angular code I use:

deleteUser(user) {
    console.log("user to delete:" + user._id);
    let myParams = new URLSearchParams();
    myParams.append('id', user._id);
    return this.http.delete('/api/user', { search: myParams })
      .map(res => res.json());
}

The correct id is being printed to console but I am not able to see even the call being made in the chrome network bar nor has the data being deleted. Any ideas what is going wrong?

like image 926
JackSlayer94 Avatar asked Oct 06 '17 09:10

JackSlayer94


People also ask

What is the use of HTTP delete method in angular?

The delete method defines the imported which means that sending that some HTTP delete request multiple times will have the same effect on the server and will not additionally affect the state. We can perform the delete operation using angular http delete method () . http delete method () URL using HTTP delete method.

How to delete an object in angular web services?

We can perform the delete operation using angular http delete method () . http delete method () URL using HTTP delete method. This is the rest web service used to update any article or information. This is of any type of object that will be passed to the rest web service server.

Which functions are used for implementation of httpclient in angular?

3 Angular HTTPClient ‘s functions are used for implementation: – post (url: string, body: any, options?: RequestOptionsArgs): Observable; – put (url: string, body: any, options?: RequestOptionsArgs): Observable; – delete (url: string, options?: RequestOptionsArgs): Observable; ...

How to make HTTP request from an angular app?

Before making HTTP requests from your Angular app you need to do a couple of things. 1. Add the HttpClientModule to the imports array of your AppModule like below on lines 3 and 10. 2. Import the HttpClient into your component and add it to the constructor () params like below on lines 2 and 8.


Video Answer


1 Answers

You have to subscribe to the call if you want it to execute. See the HttpClient documentation.

Note the subscribe() method. All Observables returned from HttpClient are cold, which is to say that they are blueprints for making requests. Nothing will happen until you call subscribe(), and every such call will make a separate request. For example, this code sends a POST request with the same data twice:

Example:

otherMethod(){
    this.userService.deleteUser(user).subscribe(() => console.log("user deleted"));
}
like image 180
Igor Avatar answered Oct 03 '22 20:10

Igor