Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor 0.5.7: how to handle/use Meteor.Collection.ObjectID?

I have updated my meteor yesterday and tried using the new Meteor.Collection.ObjectID. But since with no success. First i updated my collections in this way:

myCollection = new Meteor.Collection('mycollection', {idGeneration: 'MONGO'} Now, normal new inserts have an _id like Wi2RmR6CSapkmmdfn... (?)

Then i have a collection with an array included. I like to have an unique id for every object in this array. So i $push an object with a field like id: new Meteor.Collection.ObjectID() into my array. The result in the database is like this: ObjectId("5b5fc278305d406cc6c33756"). (This seems to be normal.)

But later i want to update my pushed object, if the id equals an id, which i stored as data attribute in an html tag before.

var equals = EJSON.equals(dbId, htmlId); (This results every time in false. So i logged the values dbId and htmlId into the console with console.log(typeof dbId, dbId);)

The values of this two variables is as follows:

object { _str: 'a86ce44f9a46b99bca1be7a9' } (dbId)

string ObjectID("a86ce44f9a46b99bca1be7a9") (htmlId; this seems to be correct, but why is a custom type a string?)

How to use the Meteor.Collection.ObjectID correct?

like image 828
net.user Avatar asked Feb 23 '13 13:02

net.user


1 Answers

When placing your htmlId in your html you need to put it in as a string and not as an object, remember _id is an object now, handlebars is guessing and using toString() & thats why it shows up as ObjectID("...").

So if you're using {{_id}} in your html you now need to use {{_id.toHexString}} to properly extract the string part of it out

When you extract this html value with your javascript you need to make it back into an objectid:

js:

var valuefromhtml = "a86ce44f9a46b99bca1be7a9"; //Get with Jquery,DOM,etc

htmlId = new Meteor.Collection.ObjectID(valuefromhtml); //see: http://docs.meteor.com/#collection_object_id

EJSON.equals(htmlId, dbId); //Should be true this time    
like image 177
Tarang Avatar answered Nov 15 '22 12:11

Tarang