question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: Cannot access 'BrandEntity before initialization

See original GitHub issue

Issue type:

[X] question [X] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [X] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

When trying to read brands (BrandEntity) I am getting the following error:

ReferenceError: Cannot access 'BrandEntity' before initializatio
import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    BaseEntity,
    ManyToOne,
    PrimaryColumn,
    JoinColumn,
    VersionColumn, OneToMany
} from "typeorm";
import {UserEntity} from "./UserEntity";
import {ResponseEntity} from "./ResponseEntity";

@Entity()
export class BrandEntity {

    @PrimaryColumn()
    id: string; 

    @Column({
        length: 150
    })
    name: string;

    @ManyToOne(type => UserEntity)
    owner: UserEntity;

    @OneToMany(type => ResponseEntity, (response: ResponseEntity) => response.brand, {
        //cascadeInsert: true,
        //cascadeUpdate: true,
        //cascadeRemove: true
        cascade: true
    })
    responses: ResponseEntity[]
}
import {Entity, PrimaryGeneratedColumn, Column, BaseEntity, ManyToOne} from "typeorm";
import {BrandEntity} from "./BrandEntity";

@Entity()
export class ResponseEntity {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        length: 1000,
        nullable: false
    })
    text: string;

    @Column({
        nullable: false
    })
    emotion: 'sad' | 'neutral' | 'happy';


    @ManyToOne(type => BrandEntity, brand => brand.responses, {
        eager: true
    })
    brand: BrandEntity;
}

Relevant config:

{
    "name": "development",
    "type": "postgres",
    "host": "localhost",
    "username": "postgres",
    "password": "postgres",
    "database": "local",
    "synchronize": true,
    "logging": true,
    "entities": [
      "./src/server/entities/**.ts"
    ],
    "migrations": [
      "./src/server/migrations/**.ts"
    ],
    "dropSchema": true
  }

Is there something wrong with my relationship? Otherwise, I believe this is a bug. Any thoughts are greatly appreciated!

Olav

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
olavencommented, Aug 20, 2020

@m4tta I ended up ditching Typeorm and going for “raw SQL” with node-pg instead. Never looked back 😄 So that’s my advice, if you are able to. EDIT: Just to be clear, Typeorm is very good at a lot of stuff! But it did not play nice with Next.js in my experience. As this discussion we’re both a part of documents.

0reactions
h666tcommented, May 18, 2022

Were you ever able to figure this out? I’m getting the same issue.

I have a similar problem and maybe it will help you. check all your relations and change them as follows.

// use string instead of function
// @ManyToOne(() => BlogSystemUser, (user) => user.blogs)
   @ManyToOne('BlogSystemUser', 'blogs')
Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >
Cannot access 'variable_name' before initialization
When you assign variables using $: you cannot assign them as part of other variables declared using let , const , or var...
Read more >
referenceerror: cannot access before initialization react
The error is telling you that the variable steps is initialized on line 7, but you're using it on line 6 to set...
Read more >
Cannot access x before initialization" - Ep 10 - YouTube
JS Casts 10 - How to fix "Uncaught ReferenceError : Cannot access x before initialization " in JavaScript.
Read more >
Javascript - referenceerror: cannot access <variable name ...
but not getting why it is giving error. It's giving - ReferenceError: Cannot access 'r1' before initialization. Please help me. What I have ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found