meteor mongodb _id changing after insert (and UUID property as well)

Posted by lommaj on Stack Overflow See other posts from Stack Overflow or by lommaj
Published on 2013-11-05T21:08:36Z Indexed on 2013/11/05 21:53 UTC
Read the original article Hit count: 191

Filed under:
|
|
|

I have meteor method that does an insert. Im using Regulate.js for form validation.

I set the game_id field to Meteor.uuid() to create a unique value that I also route to /game_show/:game_id using iron router.

As you can see I'm logging the details of the game, this works fine. (image link to log below)

Meteor.methods({
create_game_form : function(data){

    Regulate.create_game_form.validate(data, function (error, data) {
      if (error) {
        console.log('Server side validation failed.');
      } else {
        console.log('Server side validation passed!');
        // Save data to database or whatever...
        //console.log(data[0].value);
        var new_game = {
            game_id: Meteor.uuid(),
            name : data[0].value,
            game_type: data[1].value,
            creator_user_id: Meteor.userId(),
            user_name: Meteor.user().profile.name,
            created: new Date()
        };
        console.log("NEW GAME BEFORE INSERT:  ", new_game);
        GamesData.insert(new_game, function(error, new_id){

            console.log("GAMES NEW MONGO ID: ", new_id)
            var game_data = GamesData.findOne({_id: new_id});

            console.log('NEW GAME AFTER INSERT:  ', game_data);
            Session.set('CURRENT_GAME', game_data);

        });
      }
    });         
  }
 });

All of the data coming out of the console.log at this point works fine

After this method call the client routes to /game_show/:game_id

Meteor.call('create_game_form', data, function(error){  
    if(error){
        return alert(error.reason);
    }
    //console.log("post insert data for routing variable " ,data);

    var created_game = Session.get('CURRENT_GAME');

    console.log("Session Game ", created_game);
    Router.go('game_show',  {game_id: created_game.game_id});

});

On this view, I try to load the document with the game_id I just inserted

Template.game_start.helpers({
game_info: function(){      
    console.log(this.game_id);
    var game_data = GamesData.find({game_id: this.game_id});
    console.log("trying to load via UUID ", game_data);
    return game_data;
}
});

sorry cant upload images... :-(

https://www.evernote.com/shard/s21/sh/c07e8047-de93-4d08-9dc7-dae51668bdec/a8baf89a09e55f8902549e79f136fd45

As you can see from the image of the console log below, everything matches

  • the id logged before insert
  • the id logged in the insert callback using findOne()
  • the id passed in the url

However the mongo ID and the UUID I inserted ARE NOT THERE, the only document in there has all the other fields matching except those two!

Not sure what im doing wrong. Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about node.js