AngularJS reloads data after PUT application


Should be a fairly easy one here for anyone who knows Angular. I am trying to update the data that is displayed after I make a PUT request to update the object. Here is some code:

Post service (services/post.js)

'use strict';

  .factory('Post', function($resource) {
    return $resource('/api/posts/:id', {id : '@id'}, {
        'update': { method: 'PUT' }

Server side controller function that gets executed when trying to update data (lib/controllers/api.js)

exports.editsave = function(req, res, next) {
  var posty = req.body;

  console.log(posty._id.toString() + " this is posty");

  function callback (err, numAffected) {
    console.log(err + " " + numAffected);
    if(!err) {

  Post.update(posty, { id: posty._id.toString() }, callback);

This is the console output for the above code:

53c54a0d4960ddc11495d7d7 this is posty
null 0

So as you can see, it isn't affecting any of the MongoDB documents, but it also isn't producing errors.

This is what happens on the client (Angular) side when a post is updated:

$scope.saveedit = function() {
      console.log($ + " post id");
      // Now call update passing in the ID first then the object you are updating
      Post.update({ id:$ }, $, function() {$location.path('/forum')});

After the redirect, $location.path('/forum'), none of the data is displayed as being updated...when I look in the database...nothing has changed is like I am missing the step to save the changes...but I thought that update (a PUT request) would do that for me.

I use ng-init="loadposts()" when the /forum route is loaded:

$scope.loadposts = function() {
      $http.get('/api/posts').success(function (data) {$scope.posts = data});

Shouldn't all the new data be loaded after this? Any help would be appreciated. Thanks!

Your server side output indicate that the update query doesn't match any document in the database.

I'm guessing that you are using Mongoose in NodeJS server side code to connect to mongodb.

If that the case, your update statement seems incorrect.

  1. Instead of { id: .. } it should be { _id: .. }
  2. Also the conditions object and updated object are swapped.

The statement should be like this:

Post.update({ _id: posty._id.toString() }, posty, callback);

If you are not using Mongoose, please eloborate more on which library you are using or better than that, show the code where the Post variable is defined in your server side code.