类型错误:无法读取未定义的属性“belongsTo”
我尝试在两个模型“Trainer 和 Course”之间建立多对多关联,因此我创建了第三个模型“CourseTrainer”来尝试建立一对多关系。 但我收到此错误:
C:\Users\DELL\Downloads\graphql-12.18.2020\graphql-12.18.2020\graphql-12.18.2020\serverapp\database\database.js:54 db.trainer.belongsTo(db.tblCourseTrainer, { ^
TypeError:无法读取未定义的属性“belongsTo” at Object。 (C:\Users\DELL\Downloads\graphql-12.18.2020\graphql-12.18.2020\graphql-12.18.2020\serverapp\database\database.js:54:14) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) 在 Module.load (internal/modules/cjs/loader.js:928:32) 在 Function.Module._load (internal/modules/cjs/loader.js:769:14) 在 Module.require (internal/modules/cjs/loader.js:952:19) 在 require (internal/modules/cjs/helpers.js:88:18) 在 Object. (C:\Users\DELL\Downloads\graphql-12.18.2020\graphql-12.18.2020\graphql-12.18.2020\serverapp\graphql\resolvers\user.js:6:12) 在 Module._compile (internal/modules/cjs/loader.js:1063:30) 位于 Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
数据库代码:
const Sequelize = require('sequelize');
const db = {};
const sequelize = new Sequelize("firstDB"," "," ", {
//from the DB amazom
host: ' ',
dialect: 'mysql',
define: {
timestamps: false,
freezeTableName: true
},
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
});
db.sequelize = sequelize
db.Sequelize = Sequelize
db.Sequelize = sequelize
db.Sequelize = sequelize
db.Sequelize = sequelize
db.Sequelize = sequelize
db.Sequelize = sequelize
db.tblUser = require("./models/user")(sequelize, Sequelize);
db.vewStudentCourseTrainer = require("./models/studentCourseTrainer")(sequelize, Sequelize);
db.tblTrainer = require("./models/trainer")(sequelize, Sequelize);
db.tblCourse = require("./models/course")(sequelize,Sequelize);
db.tblStudent = require("./models/student")(sequelize,Sequelize);
db.tblCourseTrainer = require("./models/courseTrainer")(sequelize, Sequelize);
db.tblStudentCourse = require("./models/studentCourse")(sequelize, Sequelize);
db.tblCourseTrainer.hasMany(db.tblTrainer, { as: "trainer" });
db.trainer.belongsTo(db.tblCourseTrainer, {
foreignKey: "intTrainerID",
as: "trainerID",
});
module.exports = db
CourseTrainer 的模型代码
module.exports = (sequelize,DataTypes)=>{
const CourseTrainer = sequelize.define("tblCourseTrainer",{
intCourseTrainerID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
intCourseID: {
type: DataTypes.INTEGER,
foreignKey: true,
},
intTrainerID: {
type: DataTypes.INTEGER,
foreignKey: true,
},
},)
return CourseTrainer
}
Trainer 的模型代码
module.exports = (sequelize,DataTypes)=>{
const Trainer = sequelize.define("tblTrainer",{
intTrainerID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
strTrainerName: {
type: DataTypes.STRING
},
},)
return Trainer
}
Course 的模型代码
module.exports = (sequelize,DataTypes)=>{
const Course = sequelize.define("tblCourse",{
intCourseID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
strCourseName: {
type: DataTypes.STRING
},
},)
return Course
}
对我来说,问题在于我在不同的文件中定义了
belongsTo
关联。
解决方案是将关联移动到执行查询的同一文件中。例如
Grid.belongsToMany(Link, { through: GridLink, foreignKey: 'gridId'})
Link.belongsToMany(Grid, { through: GridLink, foreignKey: 'linkId'})
exports.listLinksInGrid = (gridId) => {
return Grid.findOne({
where: {
id: gridId
},
include: Link
});
};
而不是
Grid.belongsToMany(Link, { through: GridLink, foreignKey: 'gridId'})
exports.listLinksInGrid = (gridId) => {
return Grid.findOne({
where: {
id: gridId
},
include: Link
});
};
Link.belongsToMany(Grid, { through: GridLink, foreignKey: 'linkId'})
当您调用此方法时:
db.trainer.belongsTo(...)
db
对象中没有
.trainer
对象,因此
db.trainer
为
undefined
,并且您会收到错误消息,告诉您无法访问
undefined
值上的属性
.belongsTo()
。
我们可以在此处看到您声明
db
对象的位置:
const db = {};
然后,沿着代码往下找到您尝试执行
db.trainer.belongsTo(...)
的位置,但其中没有定义
db.trainer
。您确实有这个:
db.tblTrainer = require("./models/trainer")(sequelize, Sequelize);
那么,也许您打算使用它?
db.tblTrainer.belongsTo(...)