小编Duc*_*dru的帖子

TypeORM OneToMany 导致“ReferenceError:在初始化之前无法访问‘<Entity>’”

我有两个实体:UserHabit。一个用户可以创建多个习惯,因此我在用户上使用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> { …
Run Code Online (Sandbox Code Playgroud)

typescript typeorm nestjs nrwl-nx

9
推荐指数
4
解决办法
4489
查看次数

标签 统计

nestjs ×1

nrwl-nx ×1

typeorm ×1

typescript ×1