我有两个实体:User和Habit。用户可以创建多个习惯,因此我在用户上使用OneToMany关系(分别在习惯上使用ManyToOne)。
用户实体
import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, BeforeInsert, BeforeUpdate, OneToMany} from "typeorm";
import * as bcrypt from "bcryptjs";
import { Habit } from "../habits/habits.entity";
@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
@Column()
email: string;
@Column()
password: string;
@OneToMany(type => Habit, habit => habit.author)
habits: Habit[];
@CreateDateColumn()
dateCreated: Date;
@UpdateDateColumn()
dateUpdated: Date;
@BeforeInsert()
@BeforeUpdate()
async hashPassword(): Promise<void> {
this.password = await bcrypt.hash(this.password,10);
}
async comparePassword(password: string): Promise<boolean> {
return bcrypt.compare(password, this.password);
}
constructor(props: any) {
Object.assign(this, props);
}
}
习惯实体
import {Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne} from "typeorm";
import { User } from "../users/users.entity";
@Entity()
export class Habit {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
@Column({ nullable: true})
description?: string;
@ManyToOne(type => User, user => user.habits)
author: User;
@CreateDateColumn()
dateCreated: Date;
@UpdateDateColumn()
dateUpdated: Date;
constructor(props: Partial<Habit>) {
Object.assign(this, props);
}
}
问题
设置上述关系时,我收到以下错误
WARNING in Circular dependency detected:
apps\api\src\habits\habits.entity.ts -> apps\api\src\users\users.entity.ts -> apps\api\src\habits\habits.entity.ts
WARNING in Circular dependency detected:
apps\api\src\users\users.entity.ts -> apps\api\src\habits\habits.entity.ts -> apps\api\src\users\users.entity.ts
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "User", function() { return User; });
^
ReferenceError: Cannot access 'User' before initialization
at Module.User (...\dist\apps\api\main.js:1782:96)
at Module../apps/api/src/habits/habits.entity.ts (...\dist\apps\api\webpack:\apps\api\src\habits\habits.entity.ts:42:13)
at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
at Module../apps/api/src/users/users.entity.ts (...\dist\apps\api\main.js:1790:79)
at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
at Module../apps/api/src/config/db-config.service.ts (...\dist\apps\api\main.js:1038:77)
at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
at Module../apps/api/src/config/config.module.ts (...\dist\apps\api\main.js:978:76)
at __webpack_require__ (...\dist\apps\api\webpack:\webpack\bootstrap:19:1)
at Module../apps/api/src/app/app.module.ts (...\dist\apps\api\main.js:147:79)
笔记
我使用Nx并创建了一个NestJS应用程序。TypeOrm 版本是“^0.2.22”,@nestjs/typeorm 版本是“^6.2.0”
我的tsconfig如下:
{
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"typeRoots": ["node_modules/@types"],
"lib": ["es2018", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@awhile/contracts": ["libs/contracts/src/index.ts"],
"@awhile/ui": ["libs/ui/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
}
我尝试使用ManyToMany关系并且它有效。此外,在单独的 NestJS 应用程序(没有 Nx)上,我无法重现此参考错误。在tsconfig.json中更改 ECMAScript目标版本也不起作用。
这两个实体仅在其服务中使用,并未在其他任何地方实例化。
我很感激任何帮助。先感谢您。