Sequelize - Cannot read property 'define' of undefined

34,901

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;
Share:
34,901
salep
Author by

salep

Updated on July 09, 2022

Comments

  • salep
    salep almost 2 years

    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;
    
  • salep
    salep over 8 years
    It works, thank you. Could you please check EDIT2? I made a few changes, right now it works but I wonder something. In user.js, I put module.exports = User; , then used this in index.jsas var User = require('../models/user'); Is my User variable in the index.js looks for other one? Is that how it works?
  • Evan Siroky
    Evan Siroky over 8 years
    I don't completely understand your question, but if you are confused about what each variable is, you could always just print them out with console.log. Also, you may want to examine your new code because the callback of creating the user might not get called correctly.
  • salep
    salep over 8 years
    Okay, let's put it that way. We "throw" db in the db.js using module.exports and "catch" in the routes/index.js as db with require, is that right? Hope it's clear now :) Thank you again.
  • Evan Siroky
    Evan Siroky over 8 years
    The naming of the variables is arbitrary and you could name it whatever when you import it with require. You could say var abcd = require('../db'), sequelize = abcd.sequelize, Sequelize = abcd.Sequelize.
  • bashIt
    bashIt over 6 years
    i faced the same issue, probably there could be the typo error. example Users.create({foo: 1}). check users is correct.