Uncaught ReferenceError: Cannot access 'Model' before initialization
See original GitHub issueHello, 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:
- Created 3 years ago
- Comments:16 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
I could change the
belongsTo
API so you could have: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 !