Pages

Thursday, May 19, 2016

Learning Meteor.js [Part 22]: Beware of Meteor.Collection.ObjectID()

On startup the application needs to create relationships between the ingredients and the recipes that use them. In order to do this, when I insert the known ingredients I create their ids using Meteor.Collection.ObjectID() and then I use these when assigning an "ingredientId" field of the recipes.

This was working fine until I tried to query an ingredient by its id in minimongo:
ingredient = Ingredients.findOne(ingredientId);  // this returns undefined
ingredient = Ingredients.findOne({ '_id': ingredientId });  // so does this

So frustrating that the intuitive thing fails and I have no idea why. After reading some more posts I found that by default meteor uses a different function, namely Random.id(), to generate identifiers. After I changed to use this, the queries started to work.

I don't know what is Meteor.Collection.ObjectID() for. And at this point I don't much care, like my old team used to say "we only move forward".

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.