0

I am trying to send the ID of element to nodejs api using angularjs $resource delete method, The value is sending as parameter value how can I send that value as a body parameter?

my code is like this

In angularjs

    var ids = {
        delId : row._id
    }
    $resource('/deleteUser').delete(ids,function(data){
        alert(data)
    });

In nodejs

app.delete('/deleteUser', function(req, res) {
    console.log('called');
    var resp = {
        success : true,
        id : req.body.id
        }
    console.log(req)
    res.json(resp)
});

But Its sending only success in the response ignoring id value, How can it be done?

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • You should definitely have a `/user/:id` route instead. – moonwave99 Jan 21 '16 at 12:18
  • But my requirement is data shouldn't be in url parameters it should be send as body object just like sending data in post method – Nagababji Busam Jan 21 '16 at 12:25
  • @NagababjiBusam according to [specs](https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7) the id of the resource to DELETE must be in the URI. – Gabriele Petrioli Jan 21 '16 at 14:08
  • HI ~ if you don't mind modify the angular resource source code ~ Edit the angular-resource.js file "var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);" change to "var hasBody = /^(POST|PUT|PATCH|DELETE)$/i.test(action.method);" I think that is the best easy way . – shuyu Jan 22 '16 at 01:43
  • I have changed in resource.js file that hasBody code In browser its showing the id was send in request pay load but in backed its not showing any data in body object. For DELETE request body: {}, For POST request body: { name: 'hgasfd', phone: '8726342322', gender: 'female', email: 'ags@kja.asd' }, – Nagababji Busam Jan 22 '16 at 11:11
  • You have to add content-type header to make body parser work $resource('/deleteUser',{},{ delete:{ method: 'DELETE', headers: {'Content-Type': 'application/json'} } }).delete({"_ids":ids},function(data){ // alert(data) }); – shuyu Feb 15 '16 at 06:57

1 Answers1

0

here is what I would do for the angular part:

  angular.module('yourModule').factory('cmsUser', function($http) {

  var cmsUser = {};

  cmsUser.Delete = function(user) {
    return $http.delete("your url/", user);
  };

  return cmsUser;
});

angular.module('yourModule').factory('userService', function(cmsUser, $q) {

  var userService = {};

  userService.Delete = function(user) {
    var deferred = $q.defer();
    cmsUser.Delete(user)
      .success(function(data, status, config, headers) {
        deferred.resolve(data);
      })
      .error(function(data, status, config, headers) {
        deferred.reject(data);
      });

    return deferred.promise;
  };

  return userService;
});

angular.module('yourModule').Controller('YourController', function(userService) {

  var vm = this;

  vm.Delete = function(user) {
    userService.Delete(user)
      .then(function() {
        // this is the sucess
        console.log("user deleted");
      }, function(reason) {
        // this is the error
        console.log(reason.message);
      });
  };
});
MayK
  • 1,269
  • 1
  • 10
  • 24