我正在使用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;
}
(缩写)错误输出为:
UnhandledPromiseRejectionWarning: TypeError: relatedEntities.forEach is not a function
at OneToManySubjectBuilder.buildForSubjectRelation (/<project-directory>/src/persistence/subject-builder/OneToManySubjectBuilder.ts:78:25)
发布于 2019-08-18 05:34:37
找到了问题所在。
@TreeChildren
和@OneToMany
总是需要一个数组。我不得不将serviceData?: ServiceData;
更改为serviceData?: ServiceData[];
https://stackoverflow.com/questions/57540054
复制相似问题