Meteor method won't update collection using call/methods

I can’t get a document to update when using the following code:

client.js

// Update the story
Meteor.call('storyEdit', FlowRouter.getParam("_id"), story, function(result){

  // put stuff here

});

server.js

Meteor.methods({
  'storyEdit': function( id, doc ){

    // console.log(doc);

    // Update the user
    Stories.update(id,{$set:doc},function(error){
      if (error) {
          throw new Meteor.Error(500, error.message);
      } else {
          console.log("Update Successful");
      }
    // response
    });

  // settings
  }
});

The document has a lot of nested element in it. Four under the top most node. So like:

{  
   title:'.',
   genre:'...',
   author:'...',
   description:'...',
   image:'...',
   audio:{  
      intro:'',
      background:'...',
      outro:'...'
   },
   credits:[  
      {  
         'Story by ':'...'
      },
      {  
         'Music by ':'...'
      },
      {  
         'Illustrations by ':'...'
      }
   ],
   chapters:[  
      {  
         title:'Chapter One',
         pages:'[Object]',
         audio:{  

         }
      },
      {  
         title:'Chapter Two',
         pages:'[Object]',
         audio:{  

         }
      },
      {  
         title:'Chapter Three',
         pages:'[Object]',
         audio:{  

         }
      },
      {  
         title:'Chapter Four',
         pages:'[Object]',
         audio:{  

         }
      },
      {  
         title:'Chapter Five',
         pages:'[Object]',
         audio:{  

         }
      },
      {  
         title:'Chapter Six',
         pages:'[Object]',
         audio:{  

         }
      }
   ],
   published:false
}

The above I’ve manually serialized, so there might be mistakes

And from the terminal they’re being output as [object], but I just figured that was expected behaviour to save room on the terminal. Could this be the problem? I would expect I can just update endless child nodes (to a point).

Log outputs that the update was successful. Nothing ever changes. The terminal looks fine too.

Any thoughts?

EDIT

Working this probably backwards, I can’t get this update to update even the most basic things, like the title. Here’s an example:

export const Stories = new Mongo.Collection('stories');

if (Meteor.isServer) {
  // This code only runs on the server
  Meteor.publish('stories', function(){
    return Stories.find();
  });

}

Meteor.methods({
  'storyEdit': function( id, doc ){

    // Update the document
    Stories.update({id:'58772299f8a253cabcbc625a'},{$set:{title:"123"}},function(error,result){
      console.log(error,result);
      if (error) {
          throw new Meteor.Error(500, error.message);
      } else {
          console.log("Update Successful");
      }
    // response
    });

  // storyEdit
  }
});

And then from the terminal, just doing a simple find to see if anything has been updated:

db.stories.find({"_id" : ObjectId("58772299f8a253cabcbc625a")},{title:1});

The title, never, ever changes. What am I missing here? Could it be permissions during the edit stage? I get 0 as a result. So it’s like the two ids are totally different.

String IDs and Object IDs are different. So in your query you are selecting a string ID and in the DB console you are using an ObjectID, which is why they aren’t matching up.

Ok, so the proper nomenclature here is;

Stories.update({_id:new Mongo.Collection.ObjectID('58772299f8a253cabcbc625a')}

Is this best practice?

Yeah basically it’s up to you which type of ID you want. Also, you can use Mongo.ObjectID.

By default, Meteor uses and generates _id fields that are strings, for simplicity, but many other Mongo tools use ObjectID so we support both.

You can set which types of IDs Meteor will generate by passing an option to Mongo.Collection: http://docs.meteor.com/api/collections.html#Mongo-Collection

new Mongo.Collection('collection-name', { idGeneration: 'MONGO' } );
1 Like