6

I am not able to expose an array of objects. The Followers array is not getting exposed, even though I exposed in the UserDto

this is what I am getting,

{
    "id": "5ff4ec30-d3f4-43d3-b5ad-82b03e1c5481",
    "userName": "jdbfjl",
    "email": "[email protected]",
    "bio": "Duuude",
    "avatar": "sjldflaeulajsnlnaefb",
    "followerCount": 0,
    "followeeCount": 0,
    "verified": false,
    "followers": [
      {},
      {},
      {}
    ],
    "followees": [
      {}
    ]
  }

and expected is like

{
    "id": "5ff4ec30-d3f4-43d3-b5ad-82b03e1c5481",
    "createdAt": "2021-08-11T11:07:11.688Z",
    "updatedAt": "2021-08-11T11:07:11.688Z",
    "userName": "ashdviah",
    "email": "[email protected]",
    "bio": "I am Handsome",
    "avatar": "sjldflaeulajsnlnaefb",
    "followerCount": 0,
    "followeeCount": 0,
    "verified": false,
    "followers": [
      {
        "id": "db1d30c6-5607-4d87-8838-69f906c3c44e",
        "createdAt": "2021-08-11T11:09:33.018Z",
        "updatedAt": "2021-08-11T11:09:33.018Z"
      },
      {
        "id": "31492cd6-7c56-48f6-aff3-792a980b5100",
        "createdAt": "2021-08-11T11:11:01.288Z",
        "updatedAt": "2021-08-11T11:11:01.288Z"
      },
    ],
    "followees": [
      {
        "id": "ab095d0d-b9fa-41a4-be35-13fe9dd6f7a1",
        "createdAt": "2021-08-11T12:55:18.139Z",
        "updatedAt": "2021-08-11T12:55:18.139Z"
      }
    ]
  }

I am getting this output when I am not specifying interceptor to that route... But it turns out that I am exposing password entry with it...

my current approach is something like this : which is not working as expected... what am i missing here ?

class mock {
  @Expose() id : string;
  @Expose() createdAt : Date;
  @Expose() updatedAt : Date;
}

export class UserDto {
  @Expose()
  id : string;
  
  @Expose()
  userName : string;
  
  @Expose()
  email : string;

  @Expose()
  bio : string;

  @Expose()
  avatar : string;

  @Expose()
  followerCount : number;

  @Expose()
  followeeCount : number;

  @Expose()
  verified : boolean;

  @Expose()
  followers : Array<mock>;

  @Expose()
  followees : Array<mock>;
}

And transform is getting done by one interceptor that I used at the controller.

usage : @Serialize(UserDto) decorator

export function Serialize(dto: ClassConstructor) {
  return UseInterceptors(new Serializeinterceptor(dto));
}

export class Serializeinterceptor implements NestInterceptor {
  constructor(private dto: any) {}

  intercept(context: ExecutionContext, handler: CallHandler) {
    return handler.handle().pipe(
      map((data: any) => {
        return plainToClass(this.dto, data, {
          excludeExtraneousValues: true,
        });
      }),
    );
  }
}

1 Answer 1

12

For types that are not primitives (i.e. classes) you need to add the @Type(() => ClassType) decorator, so that class-transformer can know what it's supposed to do about the non-primitive. In this case, you need @Type(() => mock).

This is also necessary according to their docs, for any arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.