Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize - Cannot read property 'define' of undefined

I'm trying to insert a record into MYSQL table using sequelize.

Installed sequelize and mysql.

npm install --save sequelize

npm install --save mysql

defined it in app.js

var Sequelize = require('sequelize');

db.js

var Sequelize = require('sequelize');

var sequelize = new Sequelize('randomdb', 'root', 'root', {
    host: 'localhost',
    dialect: 'mysql',
    port : 8889,

    pool: {
        max: 5,
        min: 0,
        idle: 10000
    }
});

exports.sequelize = sequelize;
module.exports = Sequelize;

routes/index.js

var express = require('express');
var router = express.Router();
var sequelize = require('../db').sequelize;

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', {title: 'Express'});
});

router.get('/adduser', function (req, res) {
    var User = sequelize.define('users', {
        first_name: Sequelize.STRING
    });

    sequelize.sync().then(function () {
        return User.create({
            first_name: 'janedoe'
        });
    }).then(function (jane) {
        console.log(jane.get({
            plain: true
        }))
    });
});

module.exports = router;

Here's the error.

Cannot read property 'define' of undefined

TypeError: Cannot read property 'define' of undefined

What's missing?

EDIT 2

db.js

var Sequelize = require('sequelize');

var sequelize = new Sequelize('randomdb', 'root', 'root', {
    host: 'localhost',
    dialect: 'mysql',
    port: 8889,

    pool: {
        max: 5,
        min: 0,
        idle: 10000
    },
    define: {
        timestamps: false
    }
});

var db = {};

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

module.exports = db;

models/user.js

var db = require('../db'),
    sequelize = db.sequelize,
    Sequelize = db.Sequelize;

var User = sequelize.define('random', {
    first_name: Sequelize.STRING
});

module.exports = User;

routes/index.js

var express = require('express');
var router = express.Router();
var db = require('../db'),
    sequelize = db.sequelize,
    Sequelize = db.Sequelize;

var User = require('../models/user');

router.get('/adduseryes', function (req, res) {
    sequelize.sync().then(function () {
        return User.create({
            first_name: 'janedoe'
        });
    }).then(function (jane) {
        res.send("YES");
    });
});

module.exports = router;
like image 858
salep Avatar asked Oct 12 '15 22:10

salep


1 Answers

In your db.js file, you are overwriting the exports variable with the Sequelize var. In node.js, module.exports == exports. Try this instead:

var Sequelize = require('sequelize');

var sequelize = new Sequelize(...);

var db = {};

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

module.exports = db;

Then, you can modify your import in the routes/index.js to be as follows:

var db = require('../db'),
  sequelize = db.sequelize,
  Sequelize = db.Sequelize;
like image 144
Evan Siroky Avatar answered Nov 14 '22 02:11

Evan Siroky