meteor-collection-hooks icon indicating copy to clipboard operation
meteor-collection-hooks copied to clipboard

problem after insert doc._id object

Open aboire opened this issue 3 years ago • 4 comments

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

aboire avatar Jul 15 '22 09:07 aboire

Did you setup to use MongoDB object Id for _ids?

StorytellerCZ avatar Sep 23 '23 17:09 StorytellerCZ

@aboire can you provide a minimum reproduction or provide a code sample to test this on?

StorytellerCZ avatar Sep 24 '23 15:09 StorytellerCZ

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

aboire avatar Sep 25 '23 10:09 aboire

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) {

aboire avatar Sep 25 '23 10:09 aboire