0

I'm trying to get a handle on using $resource in angularjs and I keep referencing this answer AngularJS $resource RESTful example for good examples. Fetching a record and creating a record work fine, but now i'm trying to add a "section" to an existing mongo record but can't figure it out.

documents collection

{
  _id: 'theid', 
  name: 'My name", 
  sections: [
    {
      title: 'First title'
    },
    {
      title: 'Second title'
    }
  ]
}

angular controller snippet

var document = documentService.get({_id: 'theid'});
// TRYING TO ADD $scope.section TO THE SECTIONS ARRAY IN THE VARIABLE document.
//document.sections.push($scope.section); <-- This does NOT work
//document.new_section($scope.section); <-- could do this and then parse it out and insert it in my backend code, but this solution seems hacky and terrible to me.
document.$save(function(section) {
     //$scope.document.push(section);
});

documentService

return $resource(SETTINGS.base + '/documents/:id', { id: '@id' },
      {
        update: { method: 'PUT' }
      });

From the link i posted above, If I was just updating the name field, I could just do something like this:

var document = documentService.get({_id: 'theid'});
document.name = "My new name";    
document.$save(function(section) {
    //$scope.document.push(section);
});

I'm just trying to add an object to a nested array of objects.

8
  • $scope.document.sections.push(section) - that get call is async by the way, you need a callback! Commented Apr 24, 2014 at 20:37
  • I'm not trying to push to a scope variable, but i'm trying to set it on the document so that it gets updated in mongo. Basically from my understanding, my document variable is fetching the record from mongo, then i'm updating some of the values (or in this case i'm adding to an array), then i save that back to mongo. At least that's what i'm trying to achieve. Commented Apr 24, 2014 at 20:40
  • Have you defined the backend $save function? Commented Apr 24, 2014 at 20:48
  • $save is an angular $resource method. Commented Apr 24, 2014 at 20:49
  • I'm aware of that...it calls a method on your server tho. Commented Apr 24, 2014 at 20:52

1 Answer 1

1

Try this:

documentService.get({_id: 'theid'}, function(document) {
    document.sections.push($scope.section);
    document.$save();
});
Sign up to request clarification or add additional context in comments.

4 Comments

That seems to get me past that issue, but it's doing a POST to localhost:3000/documents/1. I would have expected it to do a PUT so that it updates a record. Is this a weird angular thing?
I believe it's the resource wiring - although I always thought put was for creating brand new objects and POST was for updating...hmm
Nope you've got it backwards.
Ahh, damn! Well, you could always be saving a brand new object, so I guess POST was the cover-all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.