meteor-collection-hooks
meteor-collection-hooks copied to clipboard
problem after insert doc._id object
with objects for _id the return of doc._id after insert is of the type "new ObjectId("964b098402035b3de292d00f")"
meteor : [email protected] matb33:[email protected] mongo server version 4.2
Did you setup to use MongoDB object Id for _ids?
@aboire can you provide a minimum reproduction or provide a code sample to test this on?
yes I configured correctly
const TestAfterInsert = new Mongo.Collection('testAfterInsert', { idGeneration: 'MONGO' });
TestAfterInsert.after.insert(function (userId, doc) {
if (doc._id instanceof Mongo.ObjectID) {
console.log("is ObjectID");
} else {
console.log("not ObjectID");
}
});
TestAfterInsert.insert({ test: 'test' });
the return is not ObjectID
I think the problem is https://github.com/Meteor-Community-Packages/meteor-collection-hooks/blob/master/insert.js#L36 I added an “else”
if (typeof id === 'object' && id.ops) {
// If _str then collection is using Mongo.ObjectID as ids
if (doc._id._str) {
id = new Mongo.ObjectID(doc._id.valueOf())
} else {
id = id.ops && id.ops[0] && id.ops[0]._id
}
} else {
if (doc && doc._id && doc._id._str) {
id = new Mongo.ObjectID(doc._id.valueOf())
}
}
but maybe an "or" here would be good but I don't know what "id.ops" corresponds to
if (typeof id === 'object' || id.ops) {