Cannot access [Model] before initialization

See original GitHub issue

Versions

  • sequelize: 4.44.4
  • sequelize-typescript: 1.1.0
  • typescript: 3.9.2

I’m submitting a …

[X] bug report [ ] feature request

Actual behavior:

$ ./node_modules/.bin/ts-node test.ts ReferenceError: Cannot access ‘Team’ before initialization

Expected behavior:

No error.

Steps to reproduce: Use (basically) the code from the README (https://github.com/RobinBuschmann/sequelize-typescript#one-to-many) as test.ts:

import {BelongsTo, Column, ForeignKey, HasMany, Model, Table} from "sequelize-typescript";

@Table
export class Player extends Model<Player> {

  @Column
  name!: string;

  @Column
  num!: number;

  @ForeignKey(() => Team)
  @Column
  teamId!: number;

  @BelongsTo(() => Team)
  team!: Team;
}

@Table
export class Team extends Model<Team> {

  @Column
  name!: string;

  @HasMany(() => Player)
  players!: Player[];
}

tsconfig.json:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
    "noImplicitAny": true,
    "module": "commonjs",
    "outDir": "_dist",
    "rootDir": ".",
    "skipLibCheck": true,
    "sourceMap": false,
    "strict": true,
    "strictNullChecks": true,
    "target": "ES2019"
  }
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:18

github_iconTop GitHub Comments

5reactions
strrifecommented, Jun 6, 2022

I’ve got another workaround.

I’ve noticed that it is possible to reference classes declared later when defining arrays

  // this works
  @HasMany(() => AuctionBid)
  public bids: AuctionBid[];

  // this does not work
  @HasOne(() => AuctionBid)
  public lastBid: AuctionBid;
                  ^ ReferenceError: Cannot access 'AuctionBid' before initialization

Which brought me to this:

  @HasOne(() => AuctionBid)
  public usersBid: ReturnType<() => AuctionBid>;

Compiles without errors.

5reactions
mschipperheyncommented, Feb 20, 2022

The root cause of all these issues is the requirement of recursive imports. This PR (https://github.com/RobinBuschmann/sequelize-typescript/pull/1206) solve that in part. But in order to completely solve this, I think the returned reference from an association should be a generic model based on the interface.

So, the final solution could look something like this:


@Table
export class Player extends Model<IPlayer> {

  @Column
  name!: string;

  @Column
  num!: number;

  @ForeignKey(models => models.Team)
  @Column
  teamId!: number;

  @BelongsTo(models => models.Team)
  team!: Model<ITeam>;
}

@Table
export class Team extends Model<ITeam> {

  @Column
  name!: string;

  @HasMany(models => models.Player)
  players!: Model<IPlayer>[];
}
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 in JS
The "Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in the...
Read more >
Cannot access [Model] before initialization · Issue #825 - GitHub
I resolved that just putting each model in different files. It is not a sequelize issue, seems like the problem is on typescript...
Read more >
ReferenceError: Cannot access 'user' before initialization
I have this error that I can't really figure out why I am getting. I use mongoose to model my db models, and...
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