1

api/profile/:accountname/:profilename

I am trying to use my api in a restful way using $resource. My data model is: each account has a list of profiles, and those profiles should have GET, POST, PUT. However, I generally don't get just one profile, rather all the profiles if I GET without profilename, then the whole account is returned.

var re = $resource('/api/profile/:accountname/:profilename', { accountname : '@accoutname', profilename : '@profilename' });
var r =  re.get({ accountname: 'leo', profilename: 'qwe' }, function (data ) {
        //change some properties on r.
        //ideally, I should be able to put r is a $scope, and have two-way binding working and $save the changes to the server.
        r.$save({ accountname: 'leo', profilename: 'qwe' });
    });

The problem with this code is that there is no data posted to the server. It was already frustrating enough that I have to pass the parameters again in the $save method. Shouldn't it remember its parameters, since I already specified it in the get method from which I got the object I am calling $save on. If there is a better way to do it please let me know. When I inspect data in the callback, it is what I expect from the server, but now I just need to be able to post whatever itself is to the server when call $save. The official doc is so minimal that it is not obvious to know what each method takes and returns, it needs a lot of improvement.

EDIT: I solved the no data posted to server problem, and it is in the comments. The mystery still remains that I have to supply parameters in my $save method while the official doc doesn't.

EDIT2: Mystery solved, see comments.

Leo Lin
  • 95
  • 1
  • 10
  • My `$resource` uses `.save()` not `.$save()`. Additionally, it returns a `$promise`. – Tony Aug 05 '14 at 21:30
  • @Tony is the documentation wrong? It uses the exact pattern leo has including $save: https://docs.angularjs.org/api/ngResource/service/$resource – Mark Aug 05 '14 at 21:31
  • Ahah! Look here: http://stackoverflow.com/a/18434501/413397 – Tony Aug 05 '14 at 21:36
  • I also just noticed that the save call is executing on `r`, which isn't a `$resource`. – Tony Aug 05 '14 at 21:41
  • `$save()` is an instance action, as opposed to `save()` which is a "class" action, to quote the docs. Just search for "non-GET instance actions" (https://docs.angularjs.org/api/ngResource/service/$resource) – Patrick Aug 05 '14 at 21:42
  • Can you see anything in your developer tools, an error in the console, or a network call that fails in the network profiler tab? Perhaps you can put a `debugger;` call before `r.$save` to see what happens when you step over it? – Patrick Aug 05 '14 at 21:43
  • I can't see a call to save with parameters anywhere in the documentation. The only parameter $save is called with is functions. Have you tried using only `r.$save()`? – Patrick Aug 05 '14 at 21:50
  • @Patrick There is no error, the $save sends a post request to /api/profile/leo/qwe with no data. – Leo Lin Aug 05 '14 at 21:50
  • @Patrick See this is what I mean the doc needs A LOT of work! If I don't supply parameters to $save, it would post to /api/profile, and that is it! I would assume it knows where it comes from, but no. I have to supply the params again. Maybe I am doing it wrong. – Leo Lin Aug 05 '14 at 21:52
  • Well have you changed anything? Resource instances track changes. It's not certain they will send anything if you don't change anything... – Patrick Aug 05 '14 at 21:52
  • @Patrick I found the problem, I was not looking at the right place from data. The whole body of the request becomes the data whereas before I had it on body.data because I was using $http manually. But it does post the data back even if there is no change to it. – Leo Lin Aug 05 '14 at 21:55
  • 1
    @Tony: r is the same instance as data. – Patrick Aug 05 '14 at 21:55
  • @Tony If you look at the Return part of the doc, it doesn't use the data from the get callback, it doesn't even have parameters in the callback. In my code, r and data are exactly the same, so I think calling $save on either one is fine. But I was just following the doc. – Leo Lin Aug 05 '14 at 22:00
  • @LeoLin My app is structured differently which was causing the confusion for me since my calls are in a service that I inject to my controller. Glad you figured it out though! – Tony Aug 05 '14 at 22:03
  • @Tony Thanks for the help, although this is mostly my fault for not looking hard enough, it is still a mystery why I have to supply the parameters in my $save call. It would post to /api/profile if I don't. – Leo Lin Aug 05 '14 at 22:05
  • If you post to `/api/profile` have you looked at the request.body? I had to do that on my server while handling the post. – Tony Aug 05 '14 at 22:07
  • @Tony I don't have such route setup in my node server because I don't want this api call. So if it is not on the url, then it is no good. – Leo Lin Aug 05 '14 at 22:10
  • @Tony I am guessing that the object I call $save on has to be from the query call, because that is what the doc shows. – Leo Lin Aug 05 '14 at 22:12
  • @LeoLin That's how it appears. But like I mentioned, I use the instance call and do `resource.save({data:'stuff'})`. – Tony Aug 05 '14 at 22:14
  • @LeoLin: This post (http://stackoverflow.com/questions/13312162/customizing-angularjs-resource-default-parameters-in-a-service) seems to address the issue you are having – Patrick Aug 05 '14 at 22:23
  • @Patrick I figured it out. @ are the property names on the object you call $save on. Either I am so careless or the doc needs more improvement. Leaning towards the former though :( – Leo Lin Aug 05 '14 at 22:45

0 Answers0