typegoose icon indicating copy to clipboard operation
typegoose copied to clipboard

Prop _id for Ref variable is undefined when updating to v11

Open angelfuel opened this issue 11 months ago • 4 comments

Hey, I know I already opened an issue when trying to update from 10.6.0 to 11.8.1. That issue was solved, but now I have found another strange one and I have no idea what can be the cause.

What is the Problem?

Lets take this code as example:

const conversation = await ConversationModel.findById("6769511d228acf0315ded894");
if (!conversation) return

console.log(conversation.participants.map(p => ({ user: p.user, id: p.user._id })))

In 10.6.0, the property _id of a Ref is equal to the same ObjectId value if the field is not populated. This is the code output:

[
  {
    user: new ObjectId("6762aa16228acf0315b89832"),
    id: new ObjectId("6762aa16228acf0315b89832")   
  },
  {
    user: new ObjectId("6399948106d482d593b16694"),
    id: new ObjectId("6399948106d482d593b16694")   
  }
]

But when updating to 11.8.1, this field becomes undefined:

[
  { user: new ObjectId("6762aa16228acf0315b89832"), id: undefined },
  { user: new ObjectId("6399948106d482d593b16694"), id: undefined }
]

Why is this a problem? Well, i know the solution is to use p.user, but when doing id equallity comparison, its very helpful to do p.user._id.toString() because with that I dont have to do extra checks to know if the model is populated or not. I know there is an equals operator, but it doesnt work with base model instances like User instead of Document<User>.For example, typescript gives error here:

async unseenMessages(@Root() e: Enrollment, @CurrentUser() currentUser: User) { // Current user from request session context
    const conversation = await ConversationModel.findById(e.conversation);
    const me = conversation.participants.find((p) => p.user.equals(currentUser)); // currentUser is not a Document<User>
}

angelfuel avatar Jan 16 '25 09:01 angelfuel

In 10.6.0, the property _id of a Ref is equal to the same ObjectId value if the field is not populated. This is the code output: But when updating to 11.8.1, this field becomes undefined:

This should still exist as mongoose adds the helper _id property to ObjectIds. Could you provide the exact versions in use for typegoose & mongoose? (yarn why PKGNAME / npm ls PKGNAME)

hasezoey avatar Jan 16 '25 10:01 hasezoey

I have pinned the exact versions in package.json before testing. These are the outputs:

└─┬ @typegoose/[email protected] └── [email protected]

└─┬ @typegoose/[email protected] └── [email protected]

angelfuel avatar Jan 16 '25 10:01 angelfuel

The versions listed still have that _id property for ObjectId and should work, and unless those versions exist at the same time, they should continue to work.

hasezoey avatar Jan 16 '25 10:01 hasezoey

The property itself exists, typescript doesnt argue when accessing _id, but it doesnt have a value when Ref field is not populated. I will try to create a repro later and will update the issue

angelfuel avatar Jan 16 '25 10:01 angelfuel

I will close this as it has been stale for over 30 days.

hasezoey avatar Aug 02 '25 11:08 hasezoey