Serialization &引用;排除“;在nestjs中未按预期工作

Serialization &引用;排除“;在nestjs中未按预期工作,serialization,nestjs,Serialization,Nestjs,我想在用户架构中排除密码字段。但是我想不出来 UserSchema.ts export type UserDocument = User & Document; @Schema() export class User extends BaseSchema { @Prop({ type: String }) firstName: string; @Prop({ type: String }) lastName: string; @Prop({

我想在用户架构中排除密码字段。但是我想不出来

UserSchema.ts

export type UserDocument = User & Document;

@Schema()
export class User extends BaseSchema {

    @Prop({ type: String })
    firstName: string;

    @Prop({ type: String })
    lastName: string;

    @Prop({ type: String, required: true, unique: true })
    email: string;

    @Prop({ type: String })
    phone: string;

    @Prop({ type: String, required: true, unique: true })
    userName: string;

    @Exclude()
    @Prop({ type: String, required: true })
    password: string;

    @Prop({ type: String })
    avatar: string;

}

export const UserSchema = SchemaFactory.createForClass(User);
UserController.ts

@UseInterceptors(ClassSerializerInterceptor)
    @Get(':id')
    public async getUserById(@Param('id') id: string): Promise<ResponseDto> {
        try {
            const result = await this.userService.getOne({ _id: id });
            return new ResponseDto({
                isSuccess: true,
                message: 'ok',
                data: result
            });

        } catch (error) {
            return new ResponseDto({
                isSuccess: false,
                message: error,
                data: null
            });
        }
    }
有两个问题我无法处理: 1-密码字段仍在响应中。我想把它排除在外 2-响应对象不干净。有很多我不想得到的领域

我期望得到的答复是:

{ 
isSuccess: true,
message:'ok',
  data: {
    "firstName": "",
    "lastName": "",
    "email": "ali@gel.com",
    "phone": "",
    "userName": "ali@gel.com",
    "avatar": ""
  }
}

如果像这样将DTO包装到另一个DTO中,则从
ClassSerializerInterceptor
的转换将不起作用
{
  "$__": {
    "strictMode": true,
    "selected": {},
    "getters": {},
    "_id": {
      "_bsontype": "ObjectID",
      "id": {
        "type": "Buffer",
        "data": [
          96,
          171,
          150,
          114,
          131,
          27,
          120,
          44,
          172,
          51,
          211,
          88
        ]
      }
    },
    "wasPopulated": false,
    "activePaths": {
      "paths": {
        "email": "init",
        "userName": "init",
        "password": "init",
        "_id": "init",
        "firstName": "init",
        "lastName": "init",
        "phone": "init",
        "avatar": "init",
        "__v": "init"
      },
      "states": {
        "ignore": {},
        "default": {},
        "init": {
          "_id": true,
          "firstName": true,
          "lastName": true,
          "email": true,
          "phone": true,
          "userName": true,
          "password": true,
          "avatar": true,
          "__v": true
        },
        "modify": {},
        "require": {}
      },
      "stateNames": [
        "require",
        "modify",
        "init",
        "default",
        "ignore"
      ]
    },
    "pathsToScopes": {},
    "cachedRequired": {},
    "$setCalled": [],
    "emitter": {
      "_events": {},
      "_eventsCount": 0,
      "_maxListeners": 0
    },
    "$options": {
      "skipId": true,
      "isNew": false,
      "willInit": true,
      "defaults": true
    }
  },
  "isNew": false,
  "$locals": {},
  "$op": null,
  "_doc": {
    "_id": {
      "_bsontype": "ObjectID",
      "id": {
        "type": "Buffer",
        "data": [
          96,
          171,
          150,
          114,
          131,
          27,
          120,
          44,
          172,
          51,
          211,
          88
        ]
      }
    },
    "firstName": "",
    "lastName": "",
    "email": "ali@gel.com",
    "phone": "",
    "userName": "ali@gel.com",
    "password": "$2b$10$Vp9EYM8fuid.bu2cisp45.QPHdQyPLLlcFeOTPUS98AzUxx3WTekG",
    "avatar": "",
    "__v": 0
  },
  "$init": true
}
{ 
isSuccess: true,
message:'ok',
  data: {
    "firstName": "",
    "lastName": "",
    "email": "ali@gel.com",
    "phone": "",
    "userName": "ali@gel.com",
    "avatar": ""
  }
}