Meteor-Community-Packages/meteor-publication-collector

Error when object _id is Meteor.Collection.ObjectID

dai-shi opened this issue · 10 comments

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.

When is this happening?

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) => { ... });

Seems you're inserting a document with an _id property; is that even possible in Mongo?

True, I did some testing as well. Changing check(id, String) to check(id, Match.OneOf(String, {_str: String})) makes sense.

That would be so great!

You might want to do:

check(id, Match.OneOf(String, Meteor.Collection.ObjectID))

I'm not sure the {_str: 'longstring'} will be recognized as an ObjectID if it comes straight from the database.

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

Awesome. Thanks. @johanbrook