Сериализация классов не работает в Nestjs - PullRequest
0 голосов
/ 13 января 2020

У меня простая пользовательская модель, и я хочу исключить из нее пароль. Используя официальные документы и ответ здесь Я пытался заставить его работать, но, похоже, это не работает, когда я получаю ответ примерно так.

[
  {
    "$__": {
      "strictMode": true,
      "selected": {},
      "getters": {},
      "_id": {
        "_bsontype": "ObjectID",
        "id": {
          "type": "Buffer",
          "data": [
            94,
            19,
            73,
            179,
            3,
            138,
            216,
            246,
            182,
            234,
            62,
            37
          ]
        }
      },
      "wasPopulated": false,
      "activePaths": {
        "paths": {
          "password": "init",
          "email": "init",
          "name": "init",
          "_id": "init",
          "__v": "init"
        },
        "states": {
          "ignore": {},
          "default": {},
          "init": {
            "_id": true,
            "name": true,
            "email": true,
            "password": true,
            "__v": true
          },
          "modify": {},
          "require": {}
        },
        "stateNames": [
          "require",
          "modify",
          "init",
          "default",
          "ignore"
        ]
      },
      "pathsToScopes": {},
      "cachedRequired": {},
      "session": null,
      "$setCalled": [],
      "emitter": {
        "_events": {},
        "_eventsCount": 0,
        "_maxListeners": 0
      },
      "$options": {
        "skipId": true,
        "isNew": false,
        "willInit": true
      }
    },
    "isNew": false,
    "_doc": {
      "_id": {
        "_bsontype": "ObjectID",
        "id": {
          "type": "Buffer",
          "data": [
            94,
            19,
            73,
            179,
            3,
            138,
            216,
            246,
            182,
            234,
            62,
            37
          ]
        }
      },
      "name": "Kamran",
      "email": "kamran@example.com",
      "password": "Pass1234",
      "__v": 0
    },
    "$locals": {},
    "$init": true
  }
]

Вот моя модель. Я использую Typegoose, но то же самое относится и к Mongoose.

export class User extends Typegoose {
  @Transform((value) => value.toString(), { toPlainOnly: true })
  _id: string;

  @prop({ required: true })
  public name!: string;

  @prop({ required: true })
  public email!: string;

  @Exclude({ toPlainOnly: true })
  @prop({ required: true })
  public password!: string;
}

Мой пользовательский сервис

@Injectable()
export class UserService {
  constructor(@InjectModel(User) private readonly user: ReturnModelType<typeof User>) {}

  async getUsers() {
    return this.user.find().exec();
  }
}

и пользовательский контроллер

@Controller('users')
@UseInterceptors(ClassSerializerInterceptor)
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get()
  async index() : Promise<User[] | []> {
    return this.userService.getUsers();
  }
}

Я пытался использовать свой пользовательский перехватчик, как описано здесь , но это не сработало, поэтому я изменил его на код ниже, как указано здесь

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(map(data => classToPlain(this.transform(data))));
  }

  transform(data) {
    const transformObject = (obj) => {
      const result = obj.toObject();
      const classProto = Object.getPrototypeOf(new User());
      Object.setPrototypeOf(result, classProto);
      return result;
    }

    return Array.isArray(data) ? data.map(obj => transformObject(obj)) : transformObject(data);
  }
}

Сейчас он работает, но код не является обобщенным c. Любой способ сделать это универсальным c?

Ответы [ 2 ]

1 голос
/ 15 января 2020

@ kamran-arshad ответ помог мне найти подходящий способ достижения sh ожидаемого результата с типом goose. Вы можете использовать декоратор @modelOptions() и передать ему объект с функцией для генерации JSON.

@modelOptions({
  toJSON: {
    transform: function(doc, ret, options) {
      delete ret.password;
      return ret;
    }
  }
})
export class User extends Typegoose {
@prop({required: true})
name!: string;

@prop({required: true})
password!: string;
}

Это не идеально, так как декораторы из class-transform не работают должным образом, но это делает работу. Кроме того, вам следует избегать использования ClassSerializerInterceptor, поскольку он даст тот же результат, что и упомянутый OP.

0 голосов
/ 15 января 2020

Я думаю, что идентифицировал проблему, но не уверен, почему это все еще происходит. Итак, вот проблема, если я возвращаю экземпляр класса, тогда сериализация работает, но если я просто возвращаю простой ответ db, возникает вышеупомянутая проблема. Итак, я обновил прототип объектов ответа в методе transform из toObject для своего пользовательского класса. Вот код.

Модель пользователя

@modelOptions({
  schemaOptions: {
    toObject: {
      transform: function(doc, ret, options) {
        Object.setPrototypeOf(ret, Object.getPrototypeOf(new User()));
      }
    },
  },
})
export class User {
  @Transform((value) => value.toString(), { toPlainOnly: true })
  public _id: string;

  @prop({ required: true })
  public name!: string;

  @prop({ required: true })
  public email!: string;

  @Exclude({ toPlainOnly: true })
  @prop({ required: true })
  public password!: string;
}

TransformInterceptor

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(map(data => classToPlain(this.transform(data))));
  }

  transform(data) {
    return Array.isArray(data) ? data.map(obj => obj.toObject()) : data.toObject();
  }
}

А теперь, если вы просто украсите свой контроллер или метод с @UseInterceptors(TransformInterceptor) это будет работать отлично. Это решение typegoose, но оно также будет работать с mongoose.

...