我正在使用Sequelize创建一个数据库来注册用户,但是在编译代码时遇到了一个问题,我得到了下面的控制台错误。
[1] C:\Users\GONZALO\Desktop\proyecto_catalogador\got-take-profit\backend\node-take-profit-api-auth\dist\models\user.js:8
[1] return _default;
[1] ^
[1]
[1] ReferenceError: Cannot access '_default' before initialization
[1] at Object.get [as default] (C:\Users\GONZALO\Desktop\proyecto_catalogador\got-take-profit\backend\node-take-profit-api-auth\dist\models\user.js:8:9)
[1] at Object.<anonymous> (C:\Users\GONZALO\Desktop\proyecto_catalogador\got-take-profit\backend\node-take-profit-api-auth\dist\models\role.js:59:26)
[1] at Module._compile (node:internal/modules/cjs/loader:1105:14)
[1] at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
[1] at Module.load (node:internal/modules/cjs/loader:981:32)
[1] at Function.Module._load (node:internal/modules/cjs/loader:822:12)
[1] at Module.require (node:internal/modules/cjs/loader:1005:19)
[1] at require (node:internal/modules/cjs/helpers:102:18)
[1] at Object.<anonymous> (C:\Users\GONZALO\Desktop\proyecto_catalogador\got-take-profit\backend\node-take-profit-api-auth\dist\models\user.js:13:54)
[1] at Module._compile (node:internal/modules/cjs/loader:1105:14)
[1] [nodemon] app crashed - waiting for file changes before starting...
字符串
导致问题的模型。
User Model:
import {Model, DataTypes} from 'sequelize';
import sequelize from '../configs/database';
import Role from './role';
import Subscription from './subscription';
class User extends Model {
public id!: number;
public username!: string;
public email!: string;
public password!: string;
public createDate!: Date;
public lastLogin!: Date;
public sessionDuration!: number;
public idRole!: number;
public idSubscription!: number;
public readonly roles?: Role[];
public readonly subscriptions?: Subscription;
}
User.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
username: {
type: DataTypes.STRING(30),
allowNull: false,
},
email: {
type: DataTypes.STRING(50),
allowNull:false,
unique: true,
validate: {
isEmail: true,
}
},
password: {
type: DataTypes.STRING(30),
allowNull: false,
},
createDate: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
lastLogin: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: DataTypes.NOW,
},
sessionDuration: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: 0,
},
idRole: {
type: DataTypes.INTEGER,
allowNull: true
},
idSubscription: {
type: DataTypes.INTEGER,
allowNull: true
}
},
{
tableName: 'users',
sequelize,
}
);
User.belongsToMany(Role, {through: 'UserRole'});
User.belongsTo(Subscription);
User.addHook('beforeCreate', async (users: User) => {
const defaultRole = await Role.findOne({where: {name: 'user'}});
if (defaultRole) {
users.idRole = defaultRole.id;
}
});
export default User;
===================================================================================
Role model:
import {Model, DataTypes} from 'sequelize';
import sequelize from '../configs/database';
import User from './user';
class Role extends Model {
public id!: number;
public name!: string;
public userId!: number;
public readonly users?: User[];
}
Role.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: DataTypes.STRING(30),
allowNull: false,
},
userId: {
type: DataTypes.INTEGER,
allowNull: true,
}
},
{
tableName: 'users',
sequelize,
}
);
Role.belongsToMany(User, {through: 'UserRole'});
export default Role;
型
下面是我的package.json和编译后的代码。
Package.json:
{
"name": "node-take-profit-api-auth",
"version": "1.0.0",
"description": "",
"main": "src/server.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "concurrently \"pnpm run watch-compile\" \"pnpm run watch-dev\"",
"watch-compile": "swc src -w --out-dir dist",
"watch-dev": "nodemon --watch \"dist/**/*\" -e js ./dist/server.js",
"build": "swc src -d dist",
"start": "NODE_ENV=production node dist/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"morgan": "^1.10.0",
"nodemon": "^3.0.1",
"pg": "^8.11.2",
"pg-hstore": "^2.3.4",
"sequelize": "^6.32.1",
"ts-node": "^10.9.1"
},
"devDependencies": {
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.73",
"@types/express": "^4.17.17",
"@types/node": "^20.4.5",
"@types/pg": "^8.10.2",
"@types/sequelize": "^4.28.15",
"concurrently": "^8.2.0",
"typescript": "^5.1.6"
}
}
Transpiled Code:
user.js:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return _default;
}
});
const _sequelize = require("sequelize");
const _database = /*#__PURE__*/ _interop_require_default(require("../configs/database"));
const _role = /*#__PURE__*/ _interop_require_default(require("./role"));
const _subscription = /*#__PURE__*/ _interop_require_default(require("./subscription"));
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
class User extends _sequelize.Model {
constructor(...args){
super(...args);
_define_property(this, "id", void 0);
_define_property(this, "username", void 0);
_define_property(this, "email", void 0);
_define_property(this, "password", void 0);
_define_property(this, "createDate", void 0);
_define_property(this, "lastLogin", void 0);
_define_property(this, "sessionDuration", void 0);
_define_property(this, "idRole", void 0);
_define_property(this, "idSubscription", void 0);
_define_property(this, "roles", void 0);
_define_property(this, "subscriptions", void 0);
}
}
User.init({
id: {
type: _sequelize.DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
username: {
type: _sequelize.DataTypes.STRING(30),
allowNull: false
},
email: {
type: _sequelize.DataTypes.STRING(50),
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: _sequelize.DataTypes.STRING(30),
allowNull: false
},
createDate: {
type: _sequelize.DataTypes.DATE,
allowNull: false,
defaultValue: _sequelize.DataTypes.NOW
},
lastLogin: {
type: _sequelize.DataTypes.DATE,
allowNull: true,
defaultValue: _sequelize.DataTypes.NOW
},
sessionDuration: {
type: _sequelize.DataTypes.BIGINT,
allowNull: false,
defaultValue: 0
},
idRole: {
type: _sequelize.DataTypes.INTEGER,
allowNull: true
},
idSubscription: {
type: _sequelize.DataTypes.INTEGER,
allowNull: true
}
}, {
tableName: 'users',
sequelize: _database.default
});
User.belongsToMany(_role.default, {
through: 'UserRole'
});
User.belongsTo(_subscription.default);
User.addHook('beforeCreate', async (users)=>{
const defaultRole = await _role.default.findOne({
where: {
name: 'user'
}
});
if (defaultRole) {
users.idRole = defaultRole.id;
}
});
const _default = User;
=====================================================================================
role.js:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return _default;
}
});
const _sequelize = require("sequelize");
const _database = /*#__PURE__*/ _interop_require_default(require("../configs/database"));
const _user = /*#__PURE__*/ _interop_require_default(require("./user"));
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
class Role extends _sequelize.Model {
constructor(...args){
super(...args);
_define_property(this, "id", void 0);
_define_property(this, "name", void 0);
_define_property(this, "userId", void 0);
_define_property(this, "users", void 0);
}
}
Role.init({
id: {
type: _sequelize.DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: _sequelize.DataTypes.STRING(30),
allowNull: false
},
userId: {
type: _sequelize.DataTypes.INTEGER,
allowNull: true
}
}, {
tableName: 'users',
sequelize: _database.default
});
Role.belongsToMany(_user.default, {
through: 'UserRole'
});
const _default = Role;
的字符串
我不能解决它,帮助是感激!
我试图修改编译的代码,但它再次编译,并出现相同的错误。
1条答案
按热度按时间nvbavucw1#
这是因为循环依赖关系(
User
导入Role
,Role
导入User
),有两种方式-1.在一个文件中定义两个模型
1.如果你对commonJS输出没问题,你可以用commonJS语法导出它,如下所示-
字符串
查看this issue了解更多详情。