I'm doing this just for study purposes and I had this doubt. I tested it by passing the DTO in a plainToClass and it works, but I wanted to understand why Nest doesn't work normally with pure DTO class.
I get this error below
[Nest] 409383 - 12/23/2024, 7:44:29 AM LOG [RouterExplorer] Mapped {/users/:id, POST} route +2ms
[Nest] 409383 - 12/23/2024, 7:44:29 AM LOG [NestApplication] Nest application successfully started +2ms
[Nest] 409383 - 12/23/2024, 7:44:35 AM ERROR [ExceptionsHandler] createUserDto.getName is not a function
TypeError: createUserDto.getName is not a function
import { Injectable } from "@nestjs/common";
import { CreateUserDto } from "./dto/create-user.dto";
@Injectable()
export class UsersService {
create(createUserDto: CreateUserDto) {
console.log(createUserDto.getName());
return {
createUserDto,
getterName: createUserDto.getName(),
};
}
}
import { IsNotEmpty, IsString } from "class-validator";
export class CreateUserDto {
@IsString()
@IsNotEmpty()
private readonly name: string;
public getName(): string {
return this.name;
}
}