1

I am using the MEAN Stack. I am trying to add a PUT request into my $ngResource and I get an error:

TypeError: conService.update is not a function

at m.$scope.joinCon (http://localhost:3000/javascripts/chirpApp-ngresource.js:217:14)

at lb.functionCall (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:200:64)

at Hc.(anonymous function).compile.d.on.f (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:216:394)

at m.$get.m.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:126:250)

at m.$get.m.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:126:476)

at HTMLButtonElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:216:446)

at HTMLButtonElement.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:32:389)

My code

//My factory
app.factory('conService', function ($resource) {return $resource('/api/cons', {'update': { method: 'PUT' }});});

//My update
conService.update({conName:$scope.cons.conName},$scope.cons);};

//My api to get all conventions
.get(function (req, res) {Con.find(function (err, cons) {if (err) { return res.send(500, err); }return res.send(cons);});})

Can anyone help me figure out what I am missing in my update? Thanks

Trewaters
  • 949
  • 3
  • 13
  • 23
  • in `$resource`, non-get functions are prefixed with `$`. does `conService.$update` work? – Claies Aug 08 '15 at 15:55
  • Unfortunately that didn't work. – Trewaters Aug 08 '15 at 16:37
  • adding {} stopped the error. Here is the updated line of code ...//My factory app.factory('conService', function ($resource) {return $resource('/api/cons', {}, {'update': { method: 'PUT' }});}); Now I get a 404 error but I think I can track that down. – Trewaters Aug 08 '15 at 17:11

1 Answers1

0

Since it's a factory you should instantiate in controller code before usage it with following:

var cs = new conService();
cs.update(/*params here*/);

For more details read those great posts: AngularJS: Service vs provider vs factory, AngularJS : Factory and Service?

Community
  • 1
  • 1
shershen
  • 9,875
  • 11
  • 39
  • 60
  • I tried directly translating what you posted into my code but it is not working. (edit: I still get the not function error about my update ) I will read the link you suggested tonight. Thanks for the additional information! – Trewaters Aug 08 '15 at 16:39