ReferenceError: 无法在初始化之前初始化用户(实体)。

huangapple go评论60阅读模式
英文:

ReferenceError: Cannot initialise user (entity) before initialization

问题

我在我的express应用程序中使用typeorm定义了3个实体 - usercompanycompanyuser。在编译时,我收到以下错误:

    at file:///C:/Users/Yu/Desktop/Projects/keytest/keytest-backend/models/CompanyUser.ts:23:9

为什么会发生这种情况,以及如何修复这个问题?

错误来自CompanyUser实体中user字段的ManyToOne装饰器,因为注释掉那一行就可以消除错误。

用户实体:

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @Column()
  email: string;

  @CreateDateColumn()
  createdAt: Date;

  @OneToMany(() => CompanyUser, companyUser => companyUser.user, { onDelete: 'CASCADE' })
  companyUsers: CompanyUser[];
}

公司实体:

export class Company extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @CreateDateColumn()
  createdAt: Date;

  @Column()
  name: string;

  @OneToMany(() => CompanyUser, companyUser => companyUser.company)
  companyUsers: CompanyUser[];
}

CompanyUser实体:

export class CompanyUser extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;
  
  @Column({ default: false })
  isVerified: boolean;

  @Column({ default: false })
  isAdmin: boolean;

  @Column({ default: false })
  isDisabled: boolean;
  
  @CreateDateColumn()
  createdAt: Date;
  
  @ManyToOne(() => User, user => user.companyUsers)
  user: User;

  @ManyToOne(() => Company, company => company.companyUsers)
  company: Company;
}

数据源:

const AppDataSource = new DataSource({
    type: "postgres",
    host: process.env.BACKEND_URL,
    port: process.env.DB_PORT as unknown as number,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    entities: [User, Company, CompanyUser],
    synchronize: true,
    logging: true,
  });
英文:

I've defined 3 entities - user, company and companyuser in my express app using typeorm. On compilation, I get the following error:

ReferenceError: Cannot access 'User' before initialization
    at file:///C:/Users/Yu/Desktop/Projects/keytest/keytest-backend/models/CompanyUser.ts:23:9

Why is this happening, and how can I fix this?

The error comes from the ManyToOne decorator for the user field in the CompanyUser entity, as commenting out that line gets rid of the error

User entity:

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @Column()
  email: string;

  @CreateDateColumn()
  createdAt: Date;

  @OneToMany(() => CompanyUser, companyUser => companyUser.user, { onDelete: 'CASCADE' })
  companyUsers: CompanyUser[];
}

Company entity:

export class Company extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @CreateDateColumn()
  createdAt: Date;

  @Column()
  name: string;

  @OneToMany(() => CompanyUser, companyUser => companyUser.company)
  companyUsers: CompanyUser[];
}

CompanyUser entity:

export class CompanyUser extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;
  
  @Column({ default: false })
  isVerified: boolean;

  @Column({ default: false })
  isAdmin: boolean;

  @Column({ default: false })
  isDisabled: boolean;
  
  @CreateDateColumn()
  createdAt: Date;
  
  @ManyToOne(() => User, user => user.companyUsers)
  user: User;

  @ManyToOne(() => Company, company => company.companyUsers)
  company: Company;
}

The datasource:

const AppDataSource = new DataSource({
    type: "postgres",
    host: process.env.BACKEND_URL,
    port: process.env.DB_PORT as unknown as number,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    entities: [User, Company, CompanyUser],
    synchronize: true,
    logging: true,
  });

答案1

得分: 1

以下是您要翻译的内容:

似乎在我的User和CompanyUser实体之间存在循环依赖,因此出现了错误。为了修复它,我添加了一个关系包装器。

根据typeorm的文档

这样做可以防止属性类型保存在属性元数据中的转译代码中,从而防止循环依赖问题。

只需记住不要在非关系列类型上使用关系包装器。

因此,CompanyUser实体变成了:

@Entity()
export class CompanyUser extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;
  
  @Column({ default: false })
  isVerified: boolean;

  @Column({ default: false })
  isAdmin: boolean;

  @Column({ default: false })
  isDisabled: boolean;
  
  @CreateDateColumn()
  createdAt: Date;
  
  @ManyToOne(() => User, (user) => user.companyUsers)
  user: Relation<User>;

  @ManyToOne(() => Company, (company) => company.companyUsers)
  company: Company;
}
英文:

It seems there was a circular dependency between my User and CompanyUser entities, hence the error. To fix it, I added a relation wrapper.

According to the typeorm docs:

> Doing this prevents the type of the property from being saved in the transpiled code in the property metadata, preventing circular dependency issues.

Just remember to not use the Relation wrapper on non-relation column types.

So the CompanyUser entity becomes:

@Entity()
export class CompanyUser extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;
  
  @Column({ default: false })
  isVerified: boolean;

  @Column({ default: false })
  isAdmin: boolean;

  @Column({ default: false })
  isDisabled: boolean;
  
  @CreateDateColumn()
  createdAt: Date;
  
  @ManyToOne(() =&gt; User, (user) =&gt; user.companyUsers)
  user: Relation&lt;User&gt;;

  @ManyToOne(() =&gt; Company, (company) =&gt; company.companyUsers)
  company: Company;
}

huangapple
  • 本文由 发表于 2023年5月7日 02:48:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定