Error when object _id is Meteor.Collection.ObjectID
dai-shi opened this issue · 10 comments
dai-shi commented
It gives the error:
Exception in queued task: Error: Match error: Expected string, got object
It's caused by check(id, String);
. How can this be avoided? Thanks.
johanbrook commented
When is this happening?
dai-shi commented
When you collect a collection. Something like the followings:
const Items = new Mongo.Collection('items');
Items.insert({ _id: new Mongo.ObjectID() });
Meteor.publish('items', () => Items.find({}));
const collector = new PublicationCollector();
collector.collect('items', (collections) => { ... });
johanbrook commented
Seems you're inserting a document with an _id
property; is that even possible in Mongo?
johanbrook commented
True, I did some testing as well. Changing check(id, String)
to check(id, Match.OneOf(String, {_str: String}))
makes sense.
dai-shi commented
That would be so great!
dai-shi commented
You might want to do:
check(id, Match.OneOf(String, Meteor.Collection.ObjectID))
johanbrook commented
I'm not sure the {_str: 'longstring'}
will be recognized as an ObjectID
if it comes straight from the database.
johanbrook commented
Nevermind, it worked:
> var d = new Mongo.Collection(null);
> d.insert({_id: new Mongo.ObjectID()})
{ _str: '417a3733019e3d7901859d2f' }
> d.findOne()._id
{ _str: '417a3733019e3d7901859d2f' }
> d.findOne()._id instanceof Mongo.ObjectID
true
dai-shi commented
Awesome. Thanks. @johanbrook