0
我正在按照文档建立n:n的关系,但是遇到以下错误:
C:\ Users \ SpiriT \ Documents \ ApiGame \ node_modules \ sequelize \ lib \ associations \ mixin.js:49 抛出新错误($ {this.name} .belongsToMany调用了某些东西 这不是Sequelize.Model的子类); ^
错误:matches.belongsToMany调用的内容不是 Sequelize.Model的子类
我的模型用户:
const { Model, DataTypes } = require ('sequelize');
const sequelize = require('../database/index');
const bcrypt = require('bcrypt');
const Matchs = require('./Matchs');
class User extends Model {}
User.init({
name: DataTypes.STRING,
email: DataTypes.STRING,
login: DataTypes.STRING,
password: DataTypes.STRING,
}, { sequelize, modelName: 'users'});
User.existLogin = (login) => {
return User.findOne({where:{login: login}});
};
User.isPassword = (encodedPassword, password) => {
return bcrypt.compareSync(password, encodedPassword);
};
User.beforeCreate = async (password) => {
const salt = await bcrypt.genSalt(10);
return bcrypt.hash(password,salt);
}
User.belongsToMany(Matchs, { foreignKey:'user_id', through:'users_matchs', as:'matchs'});
module.exports = User;
我的模特匹配
const { Model, DataTypes } = require ('sequelize');
const sequelize = require('../database/index');
const User = require('./User');
class Matchs extends Model {}
Matchs.init({
date_start: DataTypes.DATE,
date_finish: DataTypes.DATE,
}, { sequelize, modelName: 'matchs' });
Matchs.belongsToMany(User, { foreignKey:'match_id', through:'users_matchs', as:'users'});
module.exports = Matchs;