Skip to content

class-transformer not working properly #169

Open
@necm1

Description

@necm1

Hey!

I'm actually facing an issue using class-transformer using NestJS. I try to exclude some properties for my response, but getting following error:

TypeError: obj.toObject is not a function

Method I'm using to create paginate:

    const posts = await this.post.paginate(undefined, {
      limit: 10,
      page,
      populate: [
        {
          path: 'user',
          transform: (doc: any, id: any) => {
            return new User(doc);
          },
          populate: {
            path: 'profile',
            model: 'Profile',
            transform: (doc: any, id: any) => {
              return new Profile(doc);
            },
          },
        },
        {path: 'book'},
      ],
      sort: {_id: -1},
    });

The following entities User & Profile is a normal Scheme which looks for example like this:

@Schema({collection: 'users', versionKey: false})
/**
 * @class User
 */
export class User {
  @Exclude()
  /**
   * @public
   * @property
   * @type {string}
   */
  public id: string;

  @Prop({required: true, unique: true})
  /**
   * @public
   * @property
   * @type {string}
   */
  public username: string;

  @Prop({required: true})
  @Exclude()
  /**
   * @public
   * @property
   * @type {string}
   */
  public password: string;

  @Prop({required: true, unique: true})
  /**
   * @public
   * @property
   * @type {string}
   */
  public email: string;

  @Exclude()
  @Prop()
  /**
   * @public
   * @property
   * @type {Date}
   */
  public created_at: Date;

  @Prop()
  @Exclude()
  /**
   * @public
   * @property
   * @type {Date}
   */
  public updated_at: Date;

  @Prop({type: Types.ObjectId, ref: 'Profile'})
  /**
   * @public
   * @property
   * @type {Profile}
   */
  public profile: Profile | Types.ObjectId;

  /**
   * User constructor
   *
   * @constructor
   * @param {Partial<User>} partial
   */
  constructor(partial: Partial<User>) {
    Object.assign(this, partial);
  }
}

How I create the response:

    const posts = await this.postService.getHomePostsPaginate(
      query.page ? query.page : 1
    );

    const postsArray: any[] = [];

    posts.docs.forEach((v) => {
      // @TODO transform v.user & v.user.profile
      v.user = new User(v);
      v.user.profile = new Profile(v.user.profile);
      postsArray.push(v);
    });

Even without using the transform object in my populate I'm facing the same error.

I've an example where it works using findOne() (Excluding properties works):

    const user = await this.userService.getByName(req.user.username, true);
    user.profile = new Profile(user.profile);
``

Is there any reason for this behaviour? I appreciate every help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions