0
votes

I have an issue in sequelizer. i read the documentation and doing same as they did, but always stuck at the following error. please help me to solve this error.

Here is the error

TypeError: db.define is not a function at Object. (/home/robin/Public/server/models/users.js:6:17)

at Module._compile (internal/modules/cjs/loader.js:1151:30)

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)

at Module.load (internal/modules/cjs/loader.js:1000:32)

at Function.Module._load (internal/modules/cjs/loader.js:899:14)

at Module.require (internal/modules/cjs/loader.js:1040:19)

at require (internal/modules/cjs/helpers.js:72:18)

at Object. (/home/robin/Public/server/routes/user.js:4:14)

at Module._compile (internal/modules/cjs/loader.js:1151:30)

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)

at Module.load (internal/modules/cjs/loader.js:1000:32)

at Function.Module._load (internal/modules/cjs/loader.js:899:14)

at Module.require (internal/modules/cjs/loader.js:1040:19)

at require (internal/modules/cjs/helpers.js:72:18)

at Object. (/home/robin/Public/server/app.js:10:19)

at Module._compile (internal/modules/cjs/loader.js:1151:30)

[nodemon] app crashed - waiting for file changes before starting...

And Here is the code

Model

const Sequelize = require("sequelizer");
const db = require("../config/database");

const users = db.define("user", {
  email: {
    type: Sequelize.STRING
  } 
});

module.exports = users;

Database

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 config = require(__dirname + "/../config/config.json")[env];
var db = {};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
  var sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    config
  );
}

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

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

sequelize
  .authenticate()
  .then(() => {
    console.log("Connection has been established successfully.");
  })
  .catch(err => {
    console.error("Unable to connect to the database:", err);
  });

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

module.exports = db;
3

3 Answers

0
votes

I had the same problem and after doing some research it seems that when using sequelize-cli, the generated models/index.js file isn't compatible with all nodejs versions.

The workaround I found to solve this issue is to tweak index.js code into this:

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(module.filename);
const env = process.env.NODE_ENV || 'development';
const config = require('../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
  sequelize = new Sequelize(
    config.database, config.username, config.password, config
  );
}

fs
  .readdirSync(__dirname)
  .filter(file =>
    (file.indexOf('.') !== 0) &&
    (file !== basename) &&
    (file.slice(-3) === '.js'))
  .forEach(file => {
    const model = sequelize.import(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

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

module.exports = db;

Hope this helps!

0
votes

I think, It supposed to be

db.sequelize.define("user",

not

db.define("user",

const Sequelize = require("sequelizer");
const db = require("../config/database");

const users = db.sequelize.define("user", {
  email: {
    type: Sequelize.STRING
  } 
});

module.exports = users;

(or) (UPDATED)

module.exports = (sequelize, Sequelize) => {
const users = sequelize.define("user", {
      email: {
        type: Sequelize.STRING
      } 
    });
 }

Don't forgot to import the user model in Dasebase file eg:

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

    db.users = require('./path/to/usermodel.js')(sequelize, Sequelize);   

module.exports = db;
0
votes

your model

const Sequelize = require("sequelizer");
const db = require("../config/database");

module.exports = (sequelize, Sequelize) => {
const users = db.define("user", {
  email: {
    type: Sequelize.STRING
  } 
})
    return Users
}