I am using Sequelize the Node.js ORM for my web application. Facing the following issue when i start my Node server.
Following are the further details
Error:
project_directory_path\node_modules\sequelize\lib\sequelize.js:322
model.init(attributes, options);
TypeError: model.init is not a function at Sequelize.define (project_directory_path\node_modules\sequelize\lib\sequelize.js:322:11) at module.exports (project_directory_path\models\sequelize\app_config_setting.js:2:41) at Sequelize.import (project_directory_path\node_modules\sequelize\lib\sequelize.js:379:32) at project_directory_path\models\index.js:34:48 at Array.forEach (native) at Object. (project_directory_path\models\index.js:31:6) at Module._compile (module.js:425:26) at Object.Module._extensions..js (module.js:432:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Module.require (module.js:366:17) at require (module.js:385:17) at Object. (project_directory_path\server.js:12:14) at Module._compile (module.js:425:26) at Object.Module._extensions..js (module.js:432:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Function.Module.runMain (module.js:457:10) at startup (node.js:136:18) at node.js:972:3
Version:
node version v5.0.0, sequelize version: "^4.4.2", dialect: "mysql"
index.js file (to load all the models)
var fs = require('fs');
var path = require('path');
var sequelize = require('sequelize');
var sequelizeConnection =
require('../configs/dbConnection.js').exposeSequelizeConnectionObj();
var sequelizeModelDir = path.join(__dirname, 'sequelize');
var dbModels = {};
fs.readdirSync(sequelizeModelDir)
//avoiding the index.js and all other files
//expect the model files
.filter(function (file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
//iterating on each models and importing it
//in sequelize connection object
.forEach(function (file) {
var dir = 'sequelize/';
var models = sequelizeConnection.import(path.join(sequelizeModelDir, file));
dbModels[models.name] = models;
});
//exposing all the models
module.exports = dbModels;
dbModels.sequelize = sequelizeConnection;
One of my model (just to get the idea)
module.exports = function (sequelize,DataTypes) {
var roles = sequelize.define(
'roles',
{
id: {autoIncrement: true, type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true},
role_name: {type: DataTypes.ENUM('super_admin'), allowNull: false},
code_name: {type: DataTypes.ENUM('super_admin'), allowNull: false},
row_status: {type: DataTypes.ENUM('active','inactive'), allowNull: true}
},
{
timestamps: false,
paranoid: true,
freezeTableName: true,
tableName: 'roles'
}
);
return roles; };
Database Connection file:
var sequelize = require('sequelize');
var sequelizeConnectionObj = new sequelize(process.env.DB_DATABASE_NAME,
process.env.DB_USERNAME, process.env.DB_PASSWORD, {
host: process.env.DB_HOSTNAME,
dialect: 'mysql',
define: {
timestamps: false
},
dialectOptions: {
multipleStatements: true
},
logging: false,
pool: {
max: 15,
min: 0,
idle: 10000
}
});
exports.exposeSequelizeConnectionObj = function connectSequelize() {
return sequelizeConnectionObj;
};