简体   繁体   中英

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

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)

file: /lib/methods.js

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!

The issue is your code is running on the client side (or at least it looks like from the screenshot).

In meteor, Meteor.methods that run on the client side are simulation stubs. What this means is you put stuff in there that creates 'fake' data so that you can avoid the user feeling latency. This is because it would take 1-4 seconds for the server to reply with what was actually inserted in the database. This isn't really an issue though.

The reason this causes you trouble is the method is run twice (one on the server and one on the client), so it generates two different Meteor.uuid s since they are random. So this is why you have the inconsistency. What you see is the 'fake' one initially, then the server sends down the real one.

This is how Meteor makes it look like data has been inserted instantly, even though its not fully yet inserted.

To fix this get rid of the the .method you have on the client so that you only have one running on the server. You would need to get the game_id from the server though and not from the client.

If you want to keep the latency compensation, pass the Meteor.uuid in data like you do your other form data. This way the game_id will be consistent on both the server and client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM