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.

Community
  • 1
  • 1
Catfish
  • 18,876
  • 54
  • 209
  • 353
  • `$scope.document.sections.push(section)` - that `get` call is async by the way, you need a callback! – tymeJV Apr 24 '14 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. – Catfish Apr 24 '14 at 20:40
  • Have you defined the backend `$save` function? – tymeJV Apr 24 '14 at 20:48
  • $save is an angular $resource method. – Catfish Apr 24 '14 at 20:49
  • I'm aware of that...it calls a method on your server tho. – tymeJV Apr 24 '14 at 20:52
  • Yes it's defined. See the end of my question. I posted a similar working example. – Catfish Apr 24 '14 at 20:55
  • Gah, I see now. Anywaays! What happens when you try: `document.sections.push($scope.section);` - syntax error? or just doesnt save? – tymeJV Apr 24 '14 at 21:00
  • It gives me the error `Error: document.sections is undefined` – Catfish Apr 24 '14 at 21:03

1 Answers1

1

Try this:

documentService.get({_id: 'theid'}, function(document) {
    document.sections.push($scope.section);
    document.$save();
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • 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? – Catfish Apr 24 '14 at 21:11
  • I believe it's the `resource` wiring - although I always thought `put` was for creating brand new objects and `POST` was for updating...hmm – tymeJV Apr 24 '14 at 21:11
  • Nope you've got it backwards. – Catfish Apr 24 '14 at 21:12
  • Ahh, damn! Well, you could always be saving a brand new object, so I guess `POST` was the cover-all – tymeJV Apr 24 '14 at 21:14