4

Not the easiest issue to put into a title.

Anyhow, my app is built on nodejs/expressjsand has an API set up for the url:

EDIT: The current code I'm using is:

$scope.updateProduct = $resource('/api/updateProduct/:product/:param/:value',{},{
  query: {method:'GET'},
  post: {method:'POST'},
  save: {method:'PUT', params: {brand: '@brand', param:'@param', value:'@value'}},
  remove: {method:'DELETE'}
});
$scope.updateProduct.save({
    product : $scope.post._id, 
    param: 'likes', 
    value: $scope.user._id
  }); 

At present it calls /api/updateProduct instead of /api/updateProduct/<product>/<param>/<value> like it's supposed to / like it does when I perform $scope.updateProduct.get().

In my console I see (as an example):

 PUT /api/updateBrand/Quay%20Eyewear%20Australia/userTags/sunglasses,%20classic 200 30ms - 2.31kb

However, the API isn't actually accessed/nothing happens. Interestingly, if I go to localhost:5000/api/updateBrand/Quay%20Eyewear%20Australia/userTags/sunglasses,%20classic in my browser, it posts the correct data and updates the product in my database, so it's definitely an error with the way the $resource is being called.

JVG
  • 20,198
  • 47
  • 132
  • 210

3 Answers3

3

As you can see in ngResource docs, $resource receive 3 parameters:

$resource(url[, paramDefaults][, actions]);

You are passing your action as a parameter.

The correct version would be:

$scope.updateProduct = $resource('/api/updateProduct/:product/:param/:value',{}, {'save':{method:'POST'}});

Note that it isn't even necessary, because when you use $resource you already create the default methods:

{ 
    'get':    {method:'GET'},
    'save':   {method:'POST'},
    'query':  {method:'GET', isArray:true},
    'remove': {method:'DELETE'},
    'delete': {method:'DELETE'} 
};
Beterraba
  • 6,515
  • 1
  • 26
  • 33
  • Interesting, thanks. I got rid of the additional parameters in the resource call so it's just $resource('/api/updateProduct/:product/:param/:value'). However, when I do $scope.updateProduct.save() it goes to `POST /api/updateBrand 200 34ms - 2.31kb` instead of `POST /api/updateBrand/productname/paramater/value 200 34ms - 2.31kb`, thus nothing happens in the back-end. Any ideas? – JVG Oct 24 '13 at 05:22
  • You should pass the parameters in save as well, in the same way that you paste in your question: `$scope.updateProduct.save({ product : $scope.post._id, param: 'likes', value: $scope.user._id });` – Beterraba Oct 24 '13 at 13:30
  • I'm already doing that, I'll update the question above to clarify a bit better. – JVG Oct 25 '13 at 01:20
  • I'm trying to figure out the same issue you're having, with little to no luck. The answer above doesn't seem to help. – Jason Farnsworth Dec 14 '13 at 05:02
0

First of all, your have defined the save() to have a parameter called "brand", but in your url template and in the call to save(), you are using "product". I guess it's a typo.

You say when you are using browser to visit the url, it works well; but when angular is make a PUT request to the same url, nothing is happening. This indicates that your backend is configure to process only GET requests for this particular url pattern. Therefore, you need to make sure that your backend is accepting PUT requests.

Ye Liu
  • 8,946
  • 1
  • 38
  • 34
0

I was struggling with this issue and was able to pass parameters to the resource by doing the equivalent call below (notice the '$' before 'save').

$scope.updateProduct.$save({
    product : $scope.post._id, 
    param: 'likes', 
    value: $scope.user._id
});  

I also did not define a 'save' method in the resource. According to Angular docs:

"Calling these methods (meaning non-GET methods) invoke $http on the url template with the given method, params and headers. When the data is returned from the server then the object is an instance of the resource type and all of the non-GET methods are available with $ prefix. This allows you to easily support CRUD operations (create, read, update, delete) on server-side data."

trojas
  • 171
  • 1
  • 4