Sequelize ModelNotInitializedError,需要将对象添加到Sequelize实例中



我在运行项目时遇到此错误。

ModelNotInitializedError:模型未初始化:Member;getTableName"无法调用"类别";需要添加到Sequelize例子

用户模型:

import { Categorie } from './categorie.model';
import sequelize from './index'
import { Model } from 'sequelize-typescript'
interface UserAttributes {
firstname: string;
lastname: string;
email: string;
password: string;
phone: string;
address: string;
}

export class User extends Model<UserAttributes> implements UserAttributes {
public firstname!: string;
public lastname!: string;
public email!: string;
public password!: string;
public phone!: string;
public address!: string;
public getCategories!: BelongsToManyGetAssociationsMixin<Categorie>;
public addCategories!: BelongsToManyAddAssociationMixin<Categorie, number>;
public hasCategories!: BelongsToManyHasAssociationMixin<Categorie, number>;
public countCategories!: BelongsToManyCountAssociationsMixin;
public createCategories!: BelongsToManyCreateAssociationMixin<Categorie>;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
User.init({
// id: {
//   type: DataTypes.INTEGER.UNSIGNED,
//   autoIncrement: true,
//   primaryKey: true,
// },
firstname: {
type: new DataTypes.STRING(128),
allowNull: false,
},
lastname: {
type: new DataTypes.STRING(128),
allowNull: false,
},
email: {
type: new DataTypes.STRING(128),
allowNull: false,
unique: true,
},
password: {
type: new DataTypes.STRING(128),
allowNull: false,
},
phone: {
type: new DataTypes.STRING(64),
allowNull: false,
},
address: {
type: new DataTypes.STRING(256),
allowNull: false,
},
}, {
tableName: 'User',
sequelize
})
User.belongsToMany(Categorie, {through: 'User_Cat'});
Categorie.belongsToMany(User, {through: 'User_Cat'});
sequelize.sync();

分类模型:

import { BelongsToManyAddAssociationMixin, BelongsToManyCountAssociationsMixin, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyHasAssociationMixin, DataTypes } from 'sequelize';
import sequelize from './index'
import { Model } from 'sequelize-typescript'
import { Unit } from './unit.model';
interface CategorieAttributes {
index: number;
title: string;
img: string;
label: string;
eval_intro: string;
eval_mid: string;
}
export class Categorie extends Model<CategorieAttributes> implements CategorieAttributes {
public index!: number;
public title!: string;
public img!: string;
public label!: string;
public eval_intro!: string;
public eval_mid!: string;
public getUnits!: BelongsToManyGetAssociationsMixin<Unit>;
public addUnits!: BelongsToManyAddAssociationMixin<Unit, number>;
public hasUnits!: BelongsToManyHasAssociationMixin<Unit, number>;
public countUnits!: BelongsToManyCountAssociationsMixin;
public createUnits!: BelongsToManyCreateAssociationMixin<Unit>;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
Categorie.init({
// id: {
//     type: DataTypes.INTEGER.UNSIGNED,
//     autoIncrement: true,
//     primaryKey: true,
// },
index: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: true,
},
title: {
type: new DataTypes.STRING(256),
allowNull: false,
},
img: {
type: new DataTypes.STRING(256),
allowNull: true,
},
label: {
type: new DataTypes.TEXT,
allowNull: false,
},
eval_intro: {
type: new DataTypes.STRING(256),
allowNull: true,
},
eval_mid: {
type: new DataTypes.STRING(256),
allowNull: true,
}
}, {
tableName: 'Categorie',
sequelize
})
Categorie.belongsToMany(Unit, { through: 'Cat_Unit' });
Unit.belongsToMany(Categorie, { through: 'Cat_Unit' });
sequelize.sync();

关系文件:

interface UserCatAttributes {
id: number;
UserId: number;
CategorieId: number;
prog: number;
}

export class UserCat implements UserCatAttributes {
public id!: number;
public UserId!: number;
public CategorieId!: number;
public prog!: number;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}

我的主文件:

import express from 'express';
import { Express } from "express-serve-static-core";
import { Sequelize } from 'sequelize';
import { User } from './models/user.model';
import { Categorie } from './models/categorie.model';
import { Unit } from './models/unit.model';
import { UserCat } from './models/user_cat.model';
import { initRoutes } from './routes/index';
import { sequelize_config } from './config/sequelizeConfig';
import passport from 'passport';
import cors from 'cors';
const port: number = 8080;
class Beyond {
public app: Express;
public sequelize: Sequelize;
constructor() {
this.initApp()
}
initApp() {
this.initExpress()
this.initSequelize()
}
initExpress() {
this.app = express();
this.app.use(cors({
optionsSuccessStatus: 200
}))
this.app.use(express.json());
this.app.use(passport.initialize());
this.app.use(passport.authenticate('session'));
initRoutes(this.app);
this.app.listen(port, () => {
console.log("Serveur à l'écoute sur le port : ", port)
})
}
async initSequelize() {
this.sequelize = new Sequelize(
sequelize_config.db_name,
sequelize_config.db_user,
sequelize_config.db_pw,
sequelize_config.sequelize_info as any
);
await this.sequelize.authenticate().then(() => {
console.log("Connexion ok")
}).catch(err => {
console.log("err connexion : ", err)
})
}
}


export const beyond = new Beyond();

我只想做一个多对多的关系,其中User可以有很多Categorie和Categorie很多User。让我抓狂的是,在idk什么事件之前,一切都很完美,创建的表和所有后台都是用thooses模型制作的

export async function getCatByUserId(id: number): Promise<Array<Categorie>> {
const user = await User.findByPk(id, { include: Categorie });
return await user.getCategories();
}

从那时起就没有办法让它发挥作用。我是一个职业选手,所以任何帮助我都很感激。

您需要从模型模块中删除交叉引用,并定义函数来注册关联,并在所有模型都将在Sequelize实例中注册后调用它们
请在此处查看我的答案,了解如何操作。

使用

import { Sequelize } from 'sequelize'

而是

'sequelize-typescript'

它对我有用。

最新更新