问题描述
我有两个@Entity,一个是:
@Entity()
export class Person {
@PrimaryGeneratedColumn()
id!: number;
@OneToOne(type => User)
@JoinColumn()
user!: User;
@ManyToOne(type => Organization,org => org.people)
belong_organization!: Organization;
@Column("simple-array")
expertise?: string[];
}
另一个是组织:
@Entity()
export class Organization {
@PrimaryGeneratedColumn()
id!: number;
@Column('varchar')
name!: string;
@Column('text')
brief?: string;
@Column({
type: "enum",enum: ["admin","active","deactive"],default: 'active'
})
status!: string;
@Column('varchar')
website!: string;
@Column('simple-array')
mailext!: string[];
@ManyToOne(type => Person,p => p.belong_organization)
people?: Person[];
一个组织可以有很多人,也可以是空的。 当我尝试通过诸如(next.js)之类的发帖请求来构建组织时:
const handler = nextConnect();
handler.post(async (req: NextApiRequest,res: NextApiResponse)=>{
const {name,brief,status,website,mailext,member} = req.body
console.log(req.body)
const org = new Organization();
org.name = name
org.brief = brief
org.status = status
org.website = website
org.mailext = mailext
org.people = undefined
console.log("--------");
try{
const result= await dbManager.save(org) //<= Error happens here
console.log("====================");
console.log(result)
res.status(200).json(org)
}catch(e){
console.log("Error:",e)
}
})
即使我只是像[]一样将人定义为未定义或为空,仍然存在以下错误:
ReferenceError: Cannot access 'Person' before initialization
at Module.Person (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:315:98)
at Module../db/entity/Contact.ts (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:137:1152)
at __webpack_require__ (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:23:31)
at Module../db/entity/Person.ts (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:319:66)
at __webpack_require__ (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:23:31)
at Module../db/entity/Organization.ts (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:226:65)
at __webpack_require__ (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:23:31)
at Module../pages/api/organization/create.ts (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:576:81)
at __webpack_require__ (/mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:23:31)
at /mnt/c/workgit/projeny/.next/server/pages/api/organization/create.js:91:18
如何创建没有实体的空数组?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)