Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: In the list query API, each element cannot be converted to dto. #1814

Open
juyeong-s opened this issue Mar 4, 2025 · 0 comments
Open
Labels
type: question Questions about the usage of the library.

Comments

@juyeong-s
Copy link

juyeong-s commented Mar 4, 2025

I was trying to...

When searching a list in nestJS, I want to convert each item to dto.

The problem:

However, the conversion works well when doing a detailed search, but the conversion is not applied when doing a list search as shown below.

Instead of participants, the length of participants must be entered in currentParticipantCount, and instead of rinkId, an object containing the id and name taken from the rink collection must be created.
However, the document is entered as is. How do I use it?

rental.service.ts

  // detail
  async get(@Param() postId: string): Promise<RentalPostDto> {
    try {
      const post = await this.rentalPostModel
        .findOne({
          _id: postId,
        })
        .populate('rinkId', 'name')
        .lean();
      return new RentalPostDto(post);
    } catch (err) {
      console.log({ err });
      throw new Error(err);
    }
  }

// list
  async getList(
    @Query()
    { offset, limit, noun }: { offset: number; limit: number; noun?: Noun },
  ): Promise<{ list: RentalPostDto[]; isEnd: boolean }> {
    try {
      const filter = { ...(noun && { noun }) };
      const rentalList = await this.rentalPostModel
        .find(filter)
        .populate('rinkId', 'name')
        .skip(offset)
        .limit(limit);
      // .lean();
      const totalCount = await this.rentalPostModel.countDocuments();

      const list = [];
      for (const data of rentalList) {
        list.push(
          new RentalPostDto((await data.populate('rinkId', 'name')).toObject()),
        );
      }

      return {
        list: list,
        isEnd: offset + limit >= totalCount,
      };
    } catch (err) {
      console.log({ err });
      throw new Error(err);
    }
  }

rental.dto.ts

@Exclude()
export class RinkDto {
  @Transform(({ obj }) => obj._id.toString())
  @Expose()
  id: string;

  @IsString()
  @Expose()
  readonly name: string;
}

export class RentalPostDto {
  @Transform(({ obj }) => obj._id.toString())
  @Expose()
  id: string;

  @IsEnum(Noun)
  readonly noun: Noun;

  @Transform(({ obj }) => obj.rinkId)
  @Type(() => RinkDto)
  @Expose()
  readonly rink: RinkDto;

  @Exclude()
  rinkId: string;

  @IsNumber()
  readonly totalParticipantCount: number;

  @Transform(({ obj }) => obj.participants.length)
  @Expose()
  @IsNumber()
  currentParticipantCount: number;

  @Exclude()
  participants?: string[];

  @ValidateNested()
  @Type(() => PriceInfoDto)
  readonly priceInfo: PriceInfoDto;

  @ValidateNested()
  @Type(() => ReservationDateDto)
  readonly reservationDates: ReservationDateDto[];

  @IsString()
  readonly description?: string;

  @IsString()
  readonly externalChattingUrl: string;

  @Exclude()
  _id: string;

  @Exclude()
  __v: number;

  constructor(data: RentalPost) {
    Object.assign(this, data);
  }
}
@juyeong-s juyeong-s added the type: question Questions about the usage of the library. label Mar 4, 2025
@juyeong-s juyeong-s changed the title question: <your-title-goes-here> question: In the list query API, each element cannot be converted to dto. Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

1 participant