Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using the Instance methods for a resource ($get, $save, etc) will update the instance ($scope.post) in this case with whatever came back from the server.</p> <p>But I think the issue here is a bit bigger than that. Your $resource definition is not going to do anything with the ID property on the .get() method because it's not in the URL configuration. Therefore the server likely returned an array of Posts, since you went to the "/posts" resource. When you do the $save() method, Angular started with an array, and therefore takes the string that came back "OK" and creates items in the $scope.post to match those characters.</p> <p>Here's what you should change:</p> <pre><code>var Posts = $resource('/posts/:_id'); </code></pre> <p>Then: </p> <pre><code>$scope.post = Posts.get({_id: query._id}); </code></pre> <p>Now you can call:</p> <pre><code>$scope.post.$save(); </code></pre> <p>The only issue with this is that if your server returns "OK" in the body, your $scope.post will now be blown away with the return data. The options to fix this are:</p> <ol> <li><p>Return only the status code, no body, from the server. I think it would be 204 for RESTful APIs, meaning the client-side version requires no update.</p></li> <li><p>If the client-side version requires update (like a new timestamp generated on the server), return 200 and return the full body of the updated Post object.</p></li> <li><p>If you can't make the above adjustments, use the service-level method rather than the instance method:</p> <p>Posts.save({_id: query._id} , post);</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload