首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对ModelNotInitializedError进行后缀,需要将对象添加到后缀实例

对ModelNotInitializedError进行后缀,需要将对象添加到后缀实例
EN

Stack Overflow用户
提问于 2021-12-13 16:46:16
回答 1查看 1.3K关注 0票数 0

当我运行我的项目时,我会得到这个错误。

ModelNotInitializedError:未初始化的模型:不能调用"getTableName“成员。需要将“类别”添加到后缀实例中。

用户模型:

代码语言:javascript
运行
复制
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();

类别模型:

代码语言:javascript
运行
复制
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();

关系档案:

代码语言:javascript
运行
复制
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;
}

我的主要档案是:

代码语言:javascript
运行
复制
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();

我所要做的就是建立一个多到多的关系,其中用户可以拥有多个类别和多个用户。最让我抓狂的是,所有的东西都在做得很好,在idk之前,什么事件,在哪里创建的表,以及所有后端都是用thooses模型制作的。

例如

代码语言:javascript
运行
复制
export async function getCatByUserId(id: number): Promise<Array<Categorie>> {
  const user = await User.findByPk(id, { include: Categorie });
  return await user.getCategories();
}

从那以后就没有办法让它起作用了。我非常敬业,所以任何帮助都是值得感激的。

EN

Stack Overflow用户

发布于 2021-12-13 16:50:42

您需要从模型模块中删除交叉引用,并定义注册关联的函数,并在您的所有模型在Sequelize实例中注册之后调用它们。

请参阅my answer here,以了解如何进行此操作。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70338059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档