Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading