Reputation: 1033
I'm working on an E-commerce Express API, and i'm using 'sequelize' module for database manipulation. For now i have only two tables: And my project structure is as follows:
bin
-www
config
-database.js
models
-order.js
-user.js
routes
-user.js
.env
app.js
package.json
package-lock.json
database.js file:
require('dotenv').config();
const Sequelize = require('sequelize');
module.exports = new Sequelize(
process.env.DATABASE_NAME,
process.env.DATABASE_USERNAME,
process.env.DATABASE_PASSWORD, {
port: process.env.DATABASE_PORT,
host: process.env.DATABASE_HOST,
dialect: 'mysql',
}
);
User.hasMany(Order);
Order.belongsTo(User, {constraints: false});
models-user.js
const db = require('../config/database');
const Sequelize = require("sequelize");
const User = db.define('user', {
username: {
type: Sequelize.STRING(32),
unique: true,
primaryKey: true,
allowNull: false
},
email: {
type: Sequelize.STRING(254),
unique: true,
allowNull: false,
},
password: {
type: Sequelize.STRING(128),
allowNull: false,
},
/*name: {
type: Sequelize.STRING
},
isAdmin: {
type: Sequelize.BOOLEAN
},*/
}, {
freezeTableName: true,
timestamps: false,
underScored: true
});
modules.exports=User;
models-order.js
const db = require('../config/database');
const Sequelize = require('sequelize');
const Order = db.define('order', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
dateTime: {
type: Sequelize.DATE,
allowNull: false
},
totalPrice: {
type: Sequelize.INTEGER,
allowNull: false
}
}, {
freezeTableName: true,
timestamps: false,
underscored: true
}
);
module.exports=Order;
When i start the application, i get
TypeError: User.hasMany is not a function
at Object.<anonymous> (C:\Users\danfe\Documents\GitHub\USocks_Backend\config\database.js:23:6)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\Users\danfe\Documents\GitHub\USocks_Backend\models\user.js:1:74)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\Users\danfe\Documents\GitHub\USocks_Backend\config\passport.js:6:14)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
I did some googling, and it might has something to do with circular dependency? But I'm sure, i have quesitons: 1. What's wrong? 2. Am i defining the association correctly? 3. What's the correct way to difine associations?
Upvotes: 0
Views: 780
Reputation: 1553
Your runtime has no idea what is User and Order inside database.js. This is akin to using a variable without defining or initialising it at all. You need to import the User and Order model inside database.js using
const User = require("../models/user.js");
const Order = require("../models/order.js")
You can also define the association inside the models itself.
Inside models/order.js
and before module.exports do -
Order.associate = function(models) {
Order.belongsTo(models.User,{
foreignKey:user_id
})
But for this to work, you have to property initialise sequelize.
You should use the sequelize-cli to generate your config.json, models, migrations and seeds in the correct way. This way, you just need to import a single db object across your whole project and you will have access to all your models in the correct way.
Upvotes: 1
Reputation: 21
Yes, it's from circular dependency. When you call "User.hasMany(Order)" User not yet defined.Call "hasMany" and "belongTo" before place when you wanna use this relation
Upvotes: 1