shaden
shaden

Reputation: 13

TypeError: Cannot read property 'belongsTo' of undefined

I'm trying to Associations many to many, between 2 models "Trainer & Course" so I do third models "CourseTrainer" to try relation 1 to many. but I'm getting this error:

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: Cannot read property 'belongsTo' of undefined 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) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object. (C:\Users\DELL\Downloads\graphql-12.18.2020\graphql-12.18.2020\graphql-12.18.2020\serverapp\graphql\resolvers\user.js:6:12) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

DB Code:

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

Models code for 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
  }

Models code for Trainer

module.exports = (sequelize,DataTypes)=>{
    const Trainer  = sequelize.define("tblTrainer",{
      intTrainerID: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },             
      strTrainerName: {
        type: DataTypes.STRING
    },
    
    },)
    return Trainer
  }

Models code for Course


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

Upvotes: 1

Views: 4792

Answers (2)

Reese James Wahlin
Reese James Wahlin

Reputation: 21

The problem for me was that I had my belongsTo associations defined in different files.

The solution was to move the associations to the same file where the query was being done. For example

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
  });
};

instead of


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'})

Upvotes: 2

jfriend00
jfriend00

Reputation: 707486

When you call this:

db.trainer.belongsTo(...)

There is no .trainer object in the db object and thus db.trainer is undefined and you get the error telling you that you can't access a property .belongsTo() on an undefined value.

We can see where you declare the db object here:

const db = {};

And, then follow the code down to where you try to do db.trainer.belongsTo(...) and nowhere in there do you define db.trainer. You do have this:

db.tblTrainer = require("./models/trainer")(sequelize, Sequelize);

so, perhaps you meant to be using this?

db.tblTrainer.belongsTo(...)

Upvotes: 0

Related Questions