1

I've got a users list with delete button on /users url. My delete route looks like this:

app.route('/users/:userId')
    .get(users.read)
    .put(users.updateById)
    .delete(users.delete);
app.param('userId', users.userById);

But problem is, that my delete button is calling delete on /users url, so I'm getting DELETE http://localhost:3000/users 404 (Not Found). How can I solve this problem? My controller remove() function you can see below. How can I pass '/user/' + user._id to it? User is removed correctly only from scope :(

$scope.remove = function(id) {
    var user = $scope.users[id];
    var modalOptions = {
        closeButtonText: 'Cancel',
        actionButtonText: 'Delete user',
        headerText: 'Delete ' + user.displayName + '?',
        bodyText: 'Are you sure you want to delete this user?'
    };

    modalService.showModal({}, modalOptions).then(function() {
        if (user) {
            user.$remove();

            for (var i in $scope.users) {
                if ($scope.users[i] === user) {
                    $scope.users.splice(i, 1); // remove item from scope
                }
            }
        }
    });

};

user service is basic from mean.js installation

angular.module('users').factory('Users', ['$resource',
    function($resource) {
        return $resource('users', {}, {
            update: {
                method: 'PUT'
            }
        });
    }
]);
ketysek
  • 1,159
  • 1
  • 16
  • 46
  • Looks like your user.$remove method isn't setup properly. How are you defining it? – Kevin B Apr 09 '15 at 18:18
  • What settings do you think? `exports.delete` function in my server controller? – ketysek Apr 09 '15 at 18:24
  • For example, if it's using an angular resource to perform the ajax request, the resource isn't set up to properly pass the user id in the url rather than the json. Your problem is with angular at the moment, unless you want to change your server routes. What generator (if any) did you use to build your project? – Kevin B Apr 09 '15 at 18:25
  • I am using mean.js and with yo generator :) – ketysek Apr 09 '15 at 18:35
  • Can you show us your user service? – Kevin B Apr 09 '15 at 18:42
  • I pasted it in post, now I know, where is the problem :-) Could you tell me, how should I edit the service? – ketysek Apr 09 '15 at 19:07

1 Answers1

0

angular resources by default have the following methods: get, save, query, remove, delete. unless otherwise specified, they will not pass the param in the url the way you need them to. In this case, you need to specify the remove method and ensure that the param gets passed in the url.

angular.module('users').factory('Users', ['$resource',
    function($resource) {
        return $resource('users', {}, { 
            update: {
                method: 'PUT'
            },
            remove: {
                method: 'DELETE',
                url: 'users/:id',
                params: {id: '@_id'}
            }
        });
    }
]);

this assumes your user objects have an _id property, which is common in the mean stack. You should probably setup get save and update the same way.

Kevin B
  • 94,570
  • 16
  • 163
  • 180