Reputation: 43
I did refer to similar questions like this but here while debugging I am getting models
as empty. I am clueless as to why is this happening. DB table is Users
.
controllers/user.js:
const models = require('../models');
const User = models.Users;
exports.getAllUsers = (req,res,next) => {
console.log(models);
console.log(User);
User.findAll({
attributes: attributesUser
}).then(users => {
console.log(users);
res.status(200).json({
data: users
});
});
}
models/user.js:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Users = sequelize.define('Users', {
firstName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [4, 255]
}
},
middleName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [4, 255]
}
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [4,255]
}
},
mobile: {
type: DataTypes.STRING,
validate:{
len: [10,15],
isNumeric:{
msg:"Invalid Mobile Number"
}
}
},
address: {
type: DataTypes.STRING,
allowNull:false
},
dob: {
type: DataTypes.DATEONLY,
allowNull:false
},
email: {
type: DataTypes.STRING,
allowNull:false,
unique: true,
validate: {
isEmail: true
},
set(email){
this.setDataValue("email",email.toString().toLowerCase());
}
},
password: {
type: DataTypes.STRING,
allowNull:false
},
gender: {
type: DataTypes.STRING,
allowNull:false
},
isDisabled: {
type: DataTypes.BOOLEAN,
allowNull: false
},
lastLoginTime: {
type: DataTypes.DATE,
},
createdAt: {
type: DataTypes.DATE,
allowNull:false
},
updatedAt: {
type: DataTypes.DATE,
},
deletedAt: {
type: DataTypes.DATE,
},
}, {});
Users.associate = function(models) {
// associations can be defined here
Users.belongsTo(models.UserType, {
foreignKey: {
name: "userTypeId",
allowNull: false
}
});
Users.belongsTo(models.Qualification, {
foreignKey: {
name: "qualificationId",
allowNull: false
}
});
Users.belongsTo(models.Organization, {
foreignKey: "organizationId",
allowNull: false
});
};
return Users;
};
Output:
TypeError: Cannot read property 'findAll' of undefined
at exports.getAllUsers (/mnt/part1/projects/Video_Coaching/backend-v2/controllers/user.js:87:10)
at Layer.handle [as handle_request] (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/layer.js:95:5)
at next (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/layer.js:95:5)
at /mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/index.js:281:22
Upvotes: 1
Views: 1503
Reputation: 43
The problem was the models didn't had sequelize instance.
While sequelize created a index.js file under models/ but I commented that file at the start of the project because it gave me errors. I just un-commented the file and created models like this const models = require('../models/index');
.
So one should require models/index.js
to get sequelize and DataTypes instance where one is going to use models, not models/user.js.
Upvotes: 1