Nest.js | @Exclude() 装饰器在 POST 方法中不起作用

btd*_*337 4 node.js nestjs class-transformer

即使将类转换器@Exclude()库中的装饰器添加到变量中,它也会在创建对象时返回。

使用空构造函数和启用该toPlainOnly属性都会失败:

@Exclude()
password: string;
Run Code Online (Sandbox Code Playgroud)
@Exclude({ toPlainOnly: true })
password: string;
Run Code Online (Sandbox Code Playgroud)

该怎么办?

btd*_*337 12

这对我来说是工作:

使用toPlainOnly启用的属性并添加ClassSerializerInterceptor如下内容GlobalInterceptor

// your entity class

@Exclude({ toPlainOnly: true })
password: string;
Run Code Online (Sandbox Code Playgroud)
// main.ts

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

plainToClass启用全局序列化避免了始终单独使用的必要性...