Iman Tumorang Iman Tumorang - 1 month ago 35
MySQL Question

Sequelize not defined

I am using sequelize for my orm database. I'm using mysql.

I got this error when run the project.

var accounts = sequelize.define('OkAkun' , {//attrbut
^

ReferenceError: sequelize is not defined
at module.exports (/path_to_project/Backend/app/models/OkAkun.js:7:26)
at Sequelize.import (/path_to_project/Backend/node_modules/sequelize/lib/sequelize.js:691:30)
at /path_to_project/Backend/app/models/index.js:30:32
at Array.forEach (native)
at Object.<anonymous> (/path_to_project/Backend/app/models/index.js:29:3)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/path_to_project/Backend/app/routes/routers.js:4:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)


Take a look for my file


index.js


"use strict";
var fs = require('fs');
var path = require ('path');
var Sequelize = require ('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || "development";
var dbConfig =require(__base + "config/initializers/config.js").database;
var sequelize;

sequelize = new Sequelize(dbConfig[env].database , dbConfig[env].username , dbConfig[env].password,
{
"dialect": "mysql",
"host" :dbConfig[env].host,
"port" :dbConfig[env].port,
"logging": false,
"define": {underscored: true}
});

var db = {};

fs
.readdirSync(__dirname)
.filter(function(file){
return (file.indexOf(".") !== 0) && (file !== basename);
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname,file));
db[model.name] = model;
});

Object.keys(db).forEach(function (modelName){
if("associate" in db[modelName]){
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;



module.exports = db;


and this is my model. When i run the project. it show error that sequelize not defined.


OkAkun.js


module.exports = function () {

var accounts = sequelize.define('OkAkun' , {//attrbut

id_akun : {
autoIncrement: true,
type : DataTypes.INTEGER,
allowNull: false,
primaryKey : true
},
username:{
type : DataTypes.STRING,
allowNull: false
},
phone_number:{
type: DataTypes.STRING,
allowNull:false
},
email : {
type: DataTypes.STRING,
allowNull: true
},
create_at : {
type: DataTypes.DATE,
allowNull: false
},
update_at : {
type : DataTypes.DATE,
allowNull: false
},
password : {
type: DataTypes.STRING,
allowNull: false
}
});

return accounts;

};


Fyi, i'm new in using orm for the database like sequelize. And i don't understand what step i missing here.

Any thoughts?

Answer

You need to add sequelize and DataTypes to your model function's arguments. Sequelize will pass these in when you do sequelize.import.

See also https://github.com/sequelize/express-example/blob/master/models/task.js for a working example