我正在使用 TypeORM + PostgreSQL 设置服务器。将实体保存到实体存储库时,我收到错误:TypeError: relatedEntities.forEach is not a function并且实体未保存到数据库中。
这似乎只在我使用@OneToMany或@TreeChildren装饰器时才会发生。
这是导致问题的我的实体类:
import { ServiceData } from './service-data.entity';
import { ManufacturerData } from './manufacturer-data.entity';
import { Entity, Column, PrimaryGeneratedColumn, TreeChildren } from 'typeorm';
@Entity()
export class Advertisement {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: true })
name?: string;
@Column()
gatewayId: string;
@Column()
rssi: number;
@Column({ nullable: true })
mac?: string;
@TreeChildren()
manufacturerData?: ManufacturerData[];
@TreeChildren()
serviceData?: ServiceData;
}
Run Code Online (Sandbox Code Playgroud)
(缩写)错误输出为:
UnhandledPromiseRejectionWarning: TypeError: relatedEntities.forEach is not a function
at OneToManySubjectBuilder.buildForSubjectRelation (/<project-directory>/src/persistence/subject-builder/OneToManySubjectBuilder.ts:78:25)
Run Code Online (Sandbox Code Playgroud)
发现问题了。
@TreeChildren并且@OneToMany总是期待一个数组。我不得不serviceData?: ServiceData;改为serviceData?: ServiceData[];
| 归档时间: |
|
| 查看次数: |
8792 次 |
| 最近记录: |