Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

446
Views
Nestjs ClassSerializerInterceptor doesn't display _id

I have an issue properly exposing the _id using the Serializer.

I use:

@UseInterceptors(ClassSerializerInterceptor)
@SerializeOptions({ strategy: 'excludeAll' })

The defined Class:

export class UpdatedCounts {
    @Expose()
    _id: ObjectId;
    @Expose()
    aCount: number;
    @Expose()
    bCount: number;

    constructor(partial: Partial<MyDocument>) {
        Object.assign(this, partial);
    }
}

The object in console.log() before it runs through the Serializer

{
  _id: new ObjectId("61c2256ee0385774cc85a963"),
  bannerImage: 'placeholder2',
  previewImage: 'placeholder',
  aCount: 1,
  bCount: 0,
}

The object being returned:

{
  "_id": {},
  "aCount": 1,
  "bCount": 0
}

So what happened to my _id?

I tried using string type instead of ObjectId but that also does not work

I do not want to use @Exclude since there are 10 more props which I left out in the example console.log(), and it should be easier to exclude all and just use these 3

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Just use @Transform:

@Expose()
@Transform((params) => params.obj._id.toString())
_id: ObjectId;

You can not just send ObjectId with JSON. You must convert it to a string.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!