-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
ObjectId is different from Type.ObjectId #12711
Comments
are you sure you are using also did you already check that you not accidentally have multiple versions of |
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);
}
}
/*
@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);
}
}
*/ |
I think I can roughly understand that both types can be used, but only one can be used. I can't mix them. I was just wondering why the types are incompatible after upgrading for example Thanks for the reminder |
like this, The import of import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
import { ObjectId } from 'bson';
@Injectable()
export class ParseObjectIdPipe implements PipeTransform<any, ObjectId> {
transform(value: any): ObjectId {
const validObjectId: boolean = ObjectId.isValid(value);
if (!validObjectId) {
throw new BadRequestException('Invalid ObjectId');
}
return ObjectId.createFromHexString(value);
}
} |
that is because back then the mongoose Line 1749 in c44d521
but now it does and the type is not defined as optional, so the types are only one-way compatible in terms of types Lines 82 to 84 in 0eda12a
i dont know if the
just as a note, you can also just use |
yes |
I took a closer look and @hasezoey is right, this is due to the import { Types } from 'mongoose';
import { ObjectId } from 'bson';
function foo(value: any): Types.ObjectId {
return ObjectId.createFromHexString(value);
} Mongoose adds an @hasezoey is right, no need to mix bson ObjectId and Mongoose ObjectId anyway. You can do |
● Test suite failed to run src/impl/mongoose/dao/MongooseDaoFactory.ts:45:5 - error TS2322: Type 'OfferDao' is not assignable to type 'IOfferDao<ObjectId, IOffer<ObjectId>>'. Types of property 'loadSubOffers' are incompatible. Type '(parentOfferId: ObjectId) => Promise<IMongooseOffer[]>' is not assignable to type '(parentOfferId: ObjectId) => Promise<IOffer<ObjectId>[]>'. Types of parameters 'parentOfferId' and 'parentOfferId' are incompatible. Type 'ObjectId' is missing the following properties from type 'ObjectId': _bsontype, id, toHexString, toJSON, and 3 more. 45 return this.offerDao; Known issue: Automattic/mongoose#12711 Automattic/mongoose#12537
Prerequisites
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:
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
to fix it
Is that right?
I saw a problem like me
#12537
The text was updated successfully, but these errors were encountered: