I need to create a many to many association in sequelize.
I am having this error :
ReferenceError: alertSMSRule is not defined
Here is the code concerning the error :
file alertSMSrule.model.js :
module.exports = (sequelize, Sequelize) => {
const AlertSMSRule = sequelize.define("alertSMSRule", {
name: {
type: Sequelize.STRING
},
number: {
type: Sequelize.STRING
}
});
return AlertSMSRule;
};
file phoneNumber.model.js :
module.exports = (sequelize, Sequelize) => {
const PhoneNumber = sequelize.define("phoneNumber", {
name: {
type: Sequelize.STRING
},
number: {
type: Sequelize.STRING
}
});
return PhoneNumber;
};
file index.js :
db.phoneNumber = require("./phoneNumber.model.js")(sequelize, Sequelize);
db.alertSMSRule = require("./alertSMSRule.model.js")(sequelize, Sequelize);
// line below is giving the error
alertSMSRule.belongsToMany(phoneNumber, { through: 'SMSRule_PhoneNumbers' });
phoneNumber.belongsToMany(alertSMSRule, { through: 'SMSRule_PhoneNumbers' });
Sequelize is properly imported and initialized. I have a bunch of other models associations working very well, but they are all hasMany / belongsTo. This is my first belongsToMany association and it's the only one with error.
Did i miss something about creating a many to many association with sequelize ? I have checked sequelize doc and forums and everything is telling me this should work this way. Source : https://sequelize.org/docs/v6/advanced-association-concepts/advanced-many-to-many/
I found out what is the problem. As we can read on some forums, it's a Javascript error, nothing related to Node.js or Sequelize.
I replaced the code :
alertSMSRule.belongsToMany(phoneNumber, { through: 'SMSRule_PhoneNumbers' });
phoneNumber.belongsToMany(alertSMSRule, { through: 'SMSRule_PhoneNumbers' });
by :
db.alertSMSRule.belongsToMany(phoneNumber, { through: 'SMSRule_PhoneNumbers' });
db.phoneNumber.belongsToMany(alertSMSRule, { through: 'SMSRule_PhoneNumbers' });