首页 > 解决方案 > 使用 NestJS、TypeORM 和 PostgreSQL 创建实体时出错

问题描述

我在 NestJS、TypeORM 和 Postgres 中创建了一个项目,用于在大学工作,但在特定情况下,TypeORM 给了我以下错误relatedEntities.forEach is not a function

这是患者实体

@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[];
}

这是病历


@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;
}

如果我创建或列出 MedicalRecords 它可以工作,但是当我尝试创建新患者时会发生错误

标签: postgresqlnestjstypeorm

解决方案


我设法解决了这个问题,实际上问题不在代码中,而是在我发送的有效负载中,我发送了一个带有 MedicalRecord 字段的有效负载,这产生了错误,当我从有效负载中删除它时,事情开始工作了再次=)


推荐阅读