ObjectId is different from Type.ObjectId
See original GitHub issuePrerequisites
- I have written a descriptive issue title
Mongoose version
6.7.2
Node.js version
18.7.0
MongoDB version
6.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13.0.1
Issue
I want parse string to ObjectId, and Previous versions are OK
“@nestjs/common”: “^7.6.15” “mongoose”: “^5.11.12”
but there are some type errors when i upgrade version
“@nestjs/common”: “^9.2.0” “mongoose”: “^6.7.2”
code:
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
import { Types } from 'mongoose';
@Injectable()
export class ParseObjectIdPipe implements PipeTransform<any, Types.ObjectId> {
transform(value: any): Types.ObjectId {
const validObjectId: boolean = Types.ObjectId.isValid(value);
if (!validObjectId) {
throw new BadRequestException('Invalid ObjectId');
}
return Types.ObjectId.createFromHexString(value);
}
}
error:
type "import("/***/node_modules/bson/bson").ObjectID" Missing attribute in "_id",but type "import("mongoose").Types.ObjectId" is required in。
I have to use
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
import { Types } from 'mongoose';
import { ObjectId } from 'bson';
@Injectable()
export class ParseObjectIdPipe implements PipeTransform<any, ObjectId> {
transform(value: any): ObjectId {
const validObjectId: boolean = Types.ObjectId.isValid(value);
if (!validObjectId) {
throw new BadRequestException('Invalid ObjectId');
}
return Types.ObjectId.createFromHexString(value);
}
}
to fix it Is that right?
I saw a problem like me
Issue Analytics
- State:
- Created 10 months ago
- Comments:7
Top Results From Across the Web
What is the difference between mongoose.ObjectId and ...
So, mongoose.Types.ObjectId is a "mongoose object" while mongoose.ObjectId is a "mongodb object". Edit to answer the comments.
Read more >ObjectId — MongoDB Manual
If an integer value is used to create an ObjectId, the integer replaces the timestamp. ObjectId() can accept one of the following inputs:...
Read more >ObjectIds in Mongoose - Mastering JS
By default, MongoDB creates an _id property on every document that's of type ObjectId. Many other databases use a numeric id property by ......
Read more >ObjectId
A globally unique identifier for objects. ... Instances of this class are immutable. See Also: Serialized Form. MongoDB documentation: ObjectId ...
Read more >API Design in Node.js (using Express & Mongo) ObjectId
The ObjectId type is used to create primary/foreign key relationships between objects in the database. While this property looks like a String, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I took a closer look and @hasezoey is right, this is due to the
_id
getter. Here’s a simpler repro script that doesn’t involve nestjs:Mongoose adds an
_id
getter to ObjectIds to make it more convenient to work with populated paths, so you can use.path._id
to get the_id
of the document regardless of whether it is populated. But there’s no good way for Mongoose to add additional properties to a MongoDB class in TypeScript, so Mongoose’sObjectId
type extends from MongoDB driver’s.@hasezoey is right, no need to mix bson ObjectId and Mongoose ObjectId anyway. You can do
new Types.ObjectId()
. OrTypes.ObjectId.createFromHexString()
if you really want.yes
new Types.ObjectId(string)
is better , think you