plus-
plus-

Reputation: 46543

Angular $resource update method not found

I'm following the Angular main page and I've extended $resource to add the update method like this:

angular.module('user', ['ngResource']).
factory('User', function($resource) {
  var User= $resource('/user/:id', {
    update: {
      method: 'PUT'
    }
  });

  User.prototype.update = function(cb) {
    return User.update({  // this line throws the error
      id: this.id
    }, angular.extend({}, this, {
      _id: undefined
    }), cb);
  };

However running:

$scope.user.update()

throws a TypeError: Object function h(a){v(a||{},this)} has no method 'update'

I can't see what I'm missing right now, any help appreciated

Upvotes: 4

Views: 11683

Answers (3)

plus-
plus-

Reputation: 46543

I found the issue in fact I need to pass an empty object for the paramDefaults arg:

var User= $resource('/user/:id', {}, {
    update: {
      method: 'PUT'
    }

}

Upvotes: 9

qualidafial
qualidafial

Reputation: 6822

In resource instances, the method name is prefixed with $.

$scope.user.$update({}, cb);

Upvotes: 0

asgoth
asgoth

Reputation: 35829

You are using the minified angularjs version, so you have to use to array notation. Just inject the required services in an array followed by the function:

angular.module('user', ['ngResource']).
factory('User', ['$resource', function($resource) {
  var User= $resource('/user/:id', {
    update: {
      method: 'PUT'
    }
  });

  User.prototype.update = function(cb) {
    return User.update({
      id: this.id
    }, angular.extend({}, this, {
      _id: undefined
    }), cb);
  });
}]);

Note: the same applies for your directives, controllers, ...

Upvotes: 0

Related Questions