Meteor.js 1.0: How do I return an _id outside of ObjectId using this._id?

437 Views Asked by At

I'm going through the simple tutorial on the site Meteortips.com to learn about session variables, but I'm having trouble retrieving the object id.

When I follow the instructions word for word, I end up with this code:

Template.leaderboard.events({
    "click .player": function(){
      var playerId = this._id;
      Session.set("selectedPlayer", playerId);
      console.log(playerId)
    }
  })

When I log playerId, according to the tutorial, I am supposed to get the id in the form: 546d2e4e1c9a86a33e37005d, but instead, I get it in the form:

LocalCollection._ObjectID 
  {_str: "546d2e531c9a86a33e37005e", 
    toString: function, 
    equals: function, 
    clone: function, 
    typeName: function…}

I then thought to try using toString(), which ended up making the equivalency test work in a later part of the tutorial (so it solved the problem), but it still returned ObjectID("546d2e461c9a86a33e37005c") when I was expecting the id without the ObjectID() thing wrapped around it.

How can I get the id without all the extra stuff?

Edit

It might be worth noting that I did all of this on a Chromebook using Nitrous.io. I coped the code from Nitrous.io into Meteorpad here and it works like it's supposed to.

The only way I was able to get the id I wanted while in Nitrous.io was by using this._id._str instead of just this._id. I still have no idea why that is... But that's what happened.

1

There are 1 best solutions below

0
On BEST ANSWER

If you didn't give a value for _id when inserting an item to your Mongo db directly, it will use a special ObjectID for _id. If you insert an item using Meteor Collection it will use a random 17 character string by default for _id. Just add data using Meteor collections or manually set the _id to a random 17 character string when you add data.