79657137

Date: 2025-06-07 16:09:08
Score: 1
Natty:
Report link

seems like decorateors of class-transformer only work correctly with plain old JavaScript objects (POJO)
i faced same issue with @J4N

to solve this issue there are 2 options:

#1: use lean query https://mongoosejs.com/docs/tutorials/lean.html

findAll() {
    return this.categoryModel.find().sort({ sort: 1 }).lean().exec();
  }
@Get()
  async getAll() {
    const categories = await this.categoryService.findAll();
    return categories.map((cat) => new CategoryDto(cat));
  }

#2: use .toObject() https://mongoosejs.com/docs/api/document.html#Document.prototype.toObject()

async signIn(username: string, password: string): Promise<any> {
    const user = await this.usersService.findOne({ username });
    if (!user) throw new BadRequestException('invalid_payload');

    const compare = await this.usersService.verifyPassword(user, password);
    if (!compare) throw new BadRequestException('invalid_payload');

    const [accessToken, refreshToken] = await Promise.all([
      this.genAccessToken(user.id),
      this.genRefreshToken(user.id),
    ]);

    return {
      user: user.toObject(),
      accessToken,
      refreshToken,
    };
  }
@Post('login')
  async signIn(@Body() signInDto: SignInDto) {
    const { username, password } = signInDto;
    const { user, accessToken, refreshToken } = await this.authService.signIn(
      username,
      password,
    );
    return {
      user: new UserResponseDto(user),
      accessToken,
      refreshToken,
    };
  }
Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @J4N
  • Low reputation (0.5):
Posted by: Hao.Le