0

I'm developing a MEAN stack application that essentially provides an interface to make navigating through my database more straightforward. I'm fairly new to express/Node.js/AngularJS but I'm looking for a way to delete multiple items from my MongoDB through a single AngularJS $http request to my express/Node.js server.

The following works for deleting a single element from my database, but what I want to do is essentially pass an array of objects to remove from my database so I don't have to make multiple requests and speed up the process. Is there a way to do this?

$http.delete('./api/db/'+opp_id)
    .success(function(data){
        $scope.items = data;
      })
      .error(function(data){
        console.log("ERROR: " + data);
      });
awade1075
  • 57
  • 13

1 Answers1

2

This should work, if you also adapt your API to handle Arrays

var idArray = [1,2,3,4,5,6];

$scope.delete = function(idArray) {
        $http({ url: 'domain/resource', 
                method: 'DELETE', 
                data: {ids: idArray}, 
                headers: {"Content-Type": "application/json;charset=utf-8"}
        }).then(function(res) {
            console.log(res.data);
        }, function(error) {
            console.log(error);
        });
    };

But you have to know that some Application Servers do not allow a body for HTTP DELETE;

Source: Is an entity body allowed for an HTTP DELETE request?

You can also pass the array of ID's as comma-separated List in the query params.

So you should also try this approach:

    var idArray = [1,2,3,4,5,6];

    $http(
      method: 'DELETE',
      url: '/items',
      params: {
        id: JSON.stringify(idArray) 
      }
    )
Community
  • 1
  • 1
Johannes Ferner
  • 718
  • 7
  • 15