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.

ObjectId is different from Type.ObjectId

See original GitHub issue

Prerequisites

  • 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

https://github.com/Automattic/mongoose/issues/12537

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Dec 23, 2022

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:

import { Types } from 'mongoose';
import { ObjectId } from 'bson';

function foo(value: any): Types.ObjectId {
  return ObjectId.createFromHexString(value);
}

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’s ObjectId 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(). Or Types.ObjectId.createFromHexString() if you really want.

0reactions
DuJimingcommented, Dec 7, 2022

new Types.ObjectId(string)

yes new Types.ObjectId(string) is better , think you

Read more comments on GitHub >

github_iconTop 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 >

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