question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: mongoose.Types.ObjectId doesn't extend mongodb.ObjectId properly (TypeScript)

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior?

user: mongoose.Types.ObjectId;

user._id._id._id._id._id._id //...
user._id.toHexString() // error: toHexString() doesn't exist on _id

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

Version 2.6.10 changed ObjectId and Schema.index() behavior.

// before (worked)
MySchema.index({ key: 1, star: 1 }, { unique: true });

// today (not sure if it works)
MySchema.index({ key: 1, star: 1 }, { weights: { unique: true } });

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.

Mongoose: 6.2.10

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
taxiliancommented, Apr 12, 2022

I have occasionally seen typescript do things like that, where things fail but then later (after some kind of cache expires?) it works. glad that was an easy fix 😉

the _id recursion thing is from what I can tell expected.

1reaction
menesscommented, Apr 10, 2022

I temporarily rolled back to version 6.2.9 which everything works fine

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose TypeScript ObjectId casting - Type 'string' is not ...
When I try to cast string[] to Schema.Types.ObjectId[] array, the error goes away but this cannot be the solution, since it crashes at...
Read more >
Mongoose findById method not properly casting to id strings to ...
The tutorial recommends that we use mongoose.Types.ObjectId() to convert the id to a type that can be used. I implemented the mongoose.
Read more >
Mongoose v6.8.2: API docs
Used for declaring paths in your schema that should be MongoDB ObjectIds. Do not use this to create a new ObjectId instance, use...
Read more >
Strongly typed models with Mongoose and TypeScript
To do this in Mongoose you pass the type as mongoose.Schema.Types.ObjectId, having @types/mongoose installed gives your application access ...
Read more >
mongoose/index.d.ts - UNPKG
38, // eslint-disable-next-line @typescript-eslint/ban-types ... 51, * Do not use this to create a new ObjectId instance, use `mongoose.Types.ObjectId`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found