Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem after insert doc._id object #282

Open
aboire opened this issue Jul 15, 2022 · 4 comments
Open

problem after insert doc._id object #282

aboire opened this issue Jul 15, 2022 · 4 comments

Comments

@aboire
Copy link

aboire commented Jul 15, 2022

with objects for _id the return of doc._id after insert is of the type "new ObjectId("964b098402035b3de292d00f")"

meteor : METEOR@2.7.3
matb33:collection-hooks@1.1.2
mongo server version 4.2

@StorytellerCZ
Copy link
Member

Did you setup to use MongoDB object Id for _ids?

@StorytellerCZ
Copy link
Member

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

@aboire
Copy link
Author

aboire commented Sep 25, 2023

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
Copy link
Author

aboire commented Sep 25, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants