Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/65545893/nest-js-exclude-decorator-not-working-in-the-post-methods

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...