Nest.js | @Exclude() decorator not working in the POST methods

Viewed 3472

Even with @Exclude() decorator from class-transformer library added to a variable, it's being returned when object is created.

Both with empty constructor and with the toPlainOnly property enabled it's fail:

@Exclude()
password: string;
@Exclude({ toPlainOnly: true })
password: string;

What to do?

1 Answers

It's work for me:

Use toPlainOnly property enabled and too add ClassSerializerInterceptor like GlobalInterceptor:

// your entity class

@Exclude({ toPlainOnly: true })
password: string;
// main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  .
  .
  .
  app.useGlobalInterceptors(
    new ClassSerializerInterceptor(app.get(Reflector))
  );
}

Enable global serialization avoids the necessity to use plainToClass individually all time...

Related