gpt4 book ai didi

node.js - 类型错误 : Cannot read property 'belongsTo' of undefined

转载 作者:行者123 更新时间:2023-12-03 22:32:38 25 4
gpt4 key购买 nike

我正在尝试在 2 个模型“培训师和类(class)”之间建立多对多的关联,因此我使用第三个模型“CourseTrainer”来尝试 1 对多的关系。
但我收到此错误:
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, {
^
类型错误:无法读取未定义的属性“belongsTo”
在对象。 (C:\Users\DELL\Downloads\graphql-12.18.2020\graphql-12.18.2020\graphql-12.18.2020\serverapp\database\database.js:54:14)
在 Module._compile (internal/modules/cjs/loader.js:1063:30)
在 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)
在要求 (internal/modules/cjs/helpers.js:88:18)
在对象。 (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
}
培训师的型号代码
module.exports = (sequelize,DataTypes)=>{
const Trainer = sequelize.define("tblTrainer",{
intTrainerID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
strTrainerName: {
type: DataTypes.STRING
},

},)
return Trainer
}
类(class)模型代码

module.exports = (sequelize,DataTypes)=>{
const Course = sequelize.define("tblCourse",{
intCourseID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
strCourseName: {
type: DataTypes.STRING
},
},)
return Course
}

最佳答案

当你调用这个时:

db.trainer.belongsTo(...)
.trainer 对象中没有 db 对象,因此 db.trainerundefined,您会收到错误消息,告诉您无法访问 .belongsTo() 值上的属性 undefined
我们可以在这里看到你在哪里声明 db 对象:
const db = {};
并且,然后按照代码一直到您尝试执行 db.trainer.belongsTo(...) 的地方,并且在那里您没有定义 db.trainer 。你有这个:
db.tblTrainer = require("./models/trainer")(sequelize, Sequelize);
所以,也许你打算使用这个?
db.tblTrainer.belongsTo(...)

关于node.js - 类型错误 : Cannot read property 'belongsTo' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65493161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com