Relationship error for Entities in microORM and Next.js

When running my Next.js application in typescript, I get following error:

error - ReferenceError: Cannot access 'Member' before initialization

I found this specific error in the documentation: https://mikro-orm.io/docs/relationships#relations-in-esm-project. But the solution they suggest won’t work. When I try to import Rel

import { Rel } from '@mikro-orm/core';

And wrap it around the entity Member, it gives me this error:

A type referenced in a decorated signature must be imported with 'import type' or a namespace import when 'isolatedModules' and 'emitDecoratorMetadata' are enabled

When I set this options to false it still doesnt work and I get more errors.

The entity that throws the error has this code:

import { Entity, PrimaryKey, Property, ManyToOne, Rel} from "@mikro-orm/core";
import { Member } from "./Member";


@Entity()
export class File{
    
    @PrimaryKey({name: "fileId"})
    id!: number;

    @Property()
    name!: string;

    @Property()
    type!: string;

    @Property({ type: "longblob"})
    data!: Buffer;

    @Property()
    size!: number;

    @Property({name: "createdAt", type: "datetime", defaultRaw: "CURRENT_TIMESTAMP"})
    createdAt!: Date;

    @ManyToOne({entity: () => Member})
    member!: Rel<Member>;

    @Property({name: "inschrijvingsdocument"})
    isSignupDoc!: boolean;

    @Property({name: "opleidingsdocument"})
    isOpleidingsDoc!: boolean;
}
    

Is there someone who knows how to fix this?

>Solution :

To fix this issue, you can do the following:

import type { Rel } from '@mikro-orm/core';

As the error implied, except it was a little vague.

Leave a Reply