Search code examples
angularjsangular-resource

angular $resource `user.$update()` is not found


I have a click handler that changes the active flag to true. But user.$update() is doing a POST not a PUT.

What is the proper (angular) way to update a user object using $resource?

$scope.setActive = function(user) {
  User.get({ id: user._id }, function(user){
      user.active = true;
      user.$update();
    });
};

My express route should be watching for a PUT:

router.put('/:id', auth.isAuthenticated(), controller.update);

Solution

  • You can change the method of the $update function to a PUT when you're creating the service/factory:

    angular.module('app.services').factory('User', function($resource) {
      return $resource('/api/users/:id', { id: '@_id' }, {
        update: {
          method: 'PUT' // this method issues a PUT request
        }
      });
    });
    

    Check this link for more info.