I make so many tables and want to create realtionship between them. when I call the built in function of sequelize(orm) that is .hasMany . It give me the error. Cannot read the property of .hasMany of undefined.
companyModel.js
const sequelize = require("sequelize");
const companyDb = require("../../config/database");
const company1 = companyDb.define("company", {
id: {
type: sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: sequelize.STRING,
allowNull: false,
},
address: {
type: sequelize.STRING,
allowNull: false,
},
contact: {
type: sequelize.STRING,
},
primay_email: {
type: sequelize.STRING,
allowNull: false,
},
website: {
type: sequelize.STRING,
},
deleted_at: {
type: sequelize.DATE,
},
is_deleted: {
type: sequelize.BOOLEAN,
defaultValue: false,
},
logo_url: {
type: sequelize.STRING,
},
country: {
type: sequelize.STRING,
allowNull: false,
},
});
module.exports = company1;
I make the relation in the below:
relation.js
const user = require("./usermodal");
const customerCompany = require("./customerCompanyModel");
const customer = require("./customerModel");
const invoiceItems = require("./invoiceItemsModel");
const outlet = require("./outletModel");
const outletStock = require("./outletStockModel");
const product = require("./productModel");
const role = require("./roleModel");
const invoice = require("./invoicesModel");
const { use } = require("../../routes");
const {company} = require("./companyModel");
const relationshipt = company.hasMany(user);
user.belongsTo(company);
role.hasOne(user);
user.belongsTo(role);
company.hasMany(product);
product.belongsTo(company);
company.hasMany(outlet);
outlet.belongsTo(company);
company.hasMany(customerCompany);
customerCompany.belongsTo(company);
outlet.hasMany(user);
user.belongsTo(outlet);
outlet.hasMany(customerCompany);
customerCompany.belongsTo(outlet);
outlet.hasMany(invoice);
invoice.belongsTo(outlet);
outlet.hasMany(outletStock);
outletStock.belongsTo(outlet);
product.hasMany(outletStock);
outletStock.belongsTo(product);
product.hasOne(invoiceItems);
invoiceItems.belongsTo(product);
customer.hasMany(customerCompany);
customerCompany.belongsTo(customer);
customer.hasMany(invoice);
invoice.belongsTo(customer);
invoice.hasMany(invoiceItems);
invoiceItems.belongsTo(invoice)
module.exports = relations;
But it give me this error:
CodePudding user response:
I think this is how you can fix it. Currently:
const {company} = require("./companyModel");
Should be (note removed curly bracers):
const company = require("./companyModel");
Code below is just to explain what happens, do not try to use it!
So, in your ./companyModel
you export your model as the whole exports
object:
module.exports = company1;
And later you include it with curly braces, which translates roughly to this:
const {company} = company1;
// or to put it differently:
const company = company1.company; // and this is the undefined value you get
With proper require
statement it would look like this:
const company = company1; // that's your model, no more undefined