Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

447
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda