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.

Uncaught ReferenceError: Cannot access 'Model' before initialization

See original GitHub issue

Hello, I encountered this error when trying to achieve one-to-many relationship error: Uncaught ReferenceError: Cannot access 'UserModel' before initialization userId: Relationships.belongsTo(UserModel),

I have 2 models Article

import {
  DataTypes,
  Model,
  Relationships,
} from "https://deno.land/x/denodb/mod.ts";
import { UserModel } from "./user.ts";
export class ArticleModel extends Model {
  static table = "articles";
  static timestamps = true;

  static fields = {
    id: { primaryKey: true, autoIncrement: true },
    content: DataTypes.TEXT,
    title: DataTypes.STRING,
    tags: DataTypes.ENUM,
    likeCount: DataTypes.INTEGER,
    userId: Relationships.belongsTo(UserModel),
  };

  static user() {
    return this.hasOne(UserModel);
  }
}

User

import { ArticleModel } from "./article.ts";

export class UserModel extends Model {
  static table = "users";
  static timestamps = true;

  static fields = {
    id: { primaryKey: true, autoIncrement: true },
    username: DataTypes.STRING,
    password: DataTypes.STRING,
  };

  static articles() {
    return this.hasMany(ArticleModel);
  }
}

Seems like a cyclic dependency problem here because User class references Article class and vice versa. Please help

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
eveningkidcommented, Apr 17, 2021

Hey guys,

I think the way this could be done without cyclic dependencies is by taking the relationships outside of the model definition.

At the moment, if you make a one-to-one relationship (or many-to-many, etc.), it has to be done this way:

import { Relationships } from 'https://deno.land/x/denodb/mod.ts';

// After both models declarations

Relationships.oneToOne(Business, Owner);

// Before database linking

I could change the belongsTo API so you could have:

// models/article
export class Article extends Model {
  static table = "articles";
  static timestamps = true;

  static fields = {
    id: { primaryKey: true, autoIncrement: true },
    // ...
  };

  static user() {
    return this.hasOne(User);
  }
}

// app
// Create userId in Article
Relationships.belongsTo(Article, User);

Also, thank you so much for your patience. I only got back to help recently, didn’t have the time before.

Anyways, let me know what you think or feel free to suggest another syntax 😃

1reaction
siegen1192commented, Apr 18, 2021

Hey guys,

I think the way this could be done without cyclic dependencies is by taking the relationships outside of the model definition.

At the moment, if you make a one-to-one relationship (or many-to-many, etc.), it has to be done this way:

import { Relationships } from 'https://deno.land/x/denodb/mod.ts';

// After both models declarations

Relationships.oneToOne(Business, Owner);

// Before database linking

I could change the belongsTo API so you could have:

// models/article
export class Article extends Model {
  static table = "articles";
  static timestamps = true;

  static fields = {
    id: { primaryKey: true, autoIncrement: true },
    // ...
  };

  static user() {
    return this.hasOne(User);
  }
}

// app
// Create userId in Article
Relationships.belongsTo(Article, User);

Also, thank you so much for your patience. I only got back to help recently, didn’t have the time before.

Anyways, let me know what you think or feel free to suggest another syntax 😃

This is what I’m looking for !

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
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 '<something>' before ... - GitHub
I am not quite sure how to describe what the situation actually is, but the bottom line is that in some rare cases...
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