DEV Community

Discussion on: Getting TypeORM to work with Next.js and TypeScript

Collapse
 
drkgrntt profile image
Derek Garnett •

Did you experience this error?
ReferenceError: Cannot access 'User' before initialization

I'm having trouble getting past this. I think it's due to the fact that I'm using relations between entities and it's creating a circular dependency with how NextJS compiles it. I've seen it referenced in a few places online, but I haven't found any solutions that have worked for me.

Collapse
 
makanamakesstuff profile image
Makana Edwards •

I'm late, but I resolved this by using interfaces for typing instead of the Entities themselves.

User:

export interface UserData {
    id: number
    username: string
    email: string
    password: string
    meta?: UserMetaData[]
    SetPassword(): Promise<void>
}

@Entity()
export class User implements UserData {
    @PrimaryGeneratedColumn()
    declare id: number

    @Column({ unique: true })
    declare username: string

    @Column({ unique: true })
    declare email: string

    @Column()
    declare password: string

    @OneToMany(() => UserMeta, (meta) => meta.user, {
        cascade: true,
    })
    declare meta?: UserMetaData[]

    @BeforeInsert()
    async SetPassword() {
        try {
            const salt = await bcyrpt.genSalt(8)
            const hashed = await bcyrpt.hash(this.password, salt)
            this.password = hashed
        } catch (error) {
            console.error(`Failed to encrpt password for ${this.username}`)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

User Meta:

export interface UserMetaData {
    uuid?: string
    name: string
    value: string
    user?: UserData
}

@Entity()
export class UserMeta {
    @PrimaryGeneratedColumn()
    declare uuid?: string

    @Column()
    declare name: string

    @Column()
    declare value: string

    @ManyToOne(() => User, (user) => user.meta, {
        onDelete: "CASCADE",
        onUpdate: "CASCADE",
        orphanedRowAction: "delete",
    })
    @JoinColumn({ name: "user_id" })
    declare user?: UserData
}

Enter fullscreen mode Exit fullscreen mode

I hope this helps future folk!

Collapse
 
muhaimincs profile image
Muhaimin CS •

still having the same error without no solution

Collapse
 
drkgrntt profile image
Derek Garnett •

If this is the error I remember, I ended up making all my relation types partials.

Thread Thread
 
ryancraigmartin profile image
Ryan Craig Martin •

Hey Derek, mind posting an example of how you fixed this?

Thread Thread
 
drkgrntt profile image
Derek Garnett •

In short: comments!: Partial<Comment[]>. I made every relationship type to another model a Partial of it.

You can see all of my models at github.com/papyrcms/papyrcms/blob/.... The example is from the Blog model, but several are like this.

It's not elegant and I'm strongly considering moving away from TypeORM for this project. It worked beautifully in a different project with a persistent server, but not so elegantly in this one using static API routes that Next provides.