NestJS TypeORM Cannot read properties of undefined (reading 'id') from DTO

Viewed 1376

I have this issue, Cannot read properties of undefined (reading 'id') and when I remove this userId field, it works perfectly fine, so i know userId is causing the error. userId is from another entity. What am i missing? Thanks!

Here is my report.entity.ts

@Entity()
export class Report {
  @PrimaryGeneratedColumn()
  id: number;

  //other fields here
  @Column()
  mileage: number;

  @ManyToOne(() => User, (user) => user.reports, { eager: true })
  user: User;
}

Here is my report.dto.ts

export class ReportDto {
  @Expose()
  id: number;

  //other fields here
  @Transform(({ obj }) => obj.user.id,)
  @Expose()
  userId: number;
}

Here is my controller code:

@Get()
getEstimate(@Query() query: GetEstimateDto) {
  return this.reportService.createEstimate(query);
}
1 Answers

I had some problem. But i added { eager: true } and then solved my problem

Related