Error creating entity with NestJS, TypeORM and PostgreSQL

Viewed 544

I'm creating a project in NestJS, TypeORM and Postgres, for a job at college, but in a specific case the TypeORM is giving me the following error relatedEntities.forEach is not a function

This is Patient Entity

@Entity()
@Unique(['cpf'])
export class Patient extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: false, type: 'varchar', length: 100 })
  name: string;

  @Column({ nullable: false, type: 'varchar', length: 255, select: false })
  password: string;

  @Column({ nullable: false, type: 'char', length: 11 })
  cpf: string;

  @OneToMany(() => MedicalRecord, medicalRecord => medicalRecord.patient)
  medicalRecord: MedicalRecord[];
}

This is the MedicalRecord


@Entity()
export class MedicalRecord extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: true, type: 'text' })
  diseaseHistory: string;

  @Column({ nullable: true, type: 'text' })
  medicines: string;

  @ManyToOne(() => Patient, patient => patient.medicalRecord)
  patient: Patient;
}

If I create or list MedicalRecords it works, but the error happens when I try to create a new Patient

1 Answers

I managed to solve the problem, in fact the problem was not in the code, but in the payload I was sending, I was sending a payload with the medicalRecord field and this generated the error, when I removed it from the payload things started working again =)

Related