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;
Author by
salep
Updated on July 09, 2022Comments
-
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 over 8 yearsIt 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 putmodule.exports = User;
, then used this inindex.js
asvar User = require('../models/user');
Is myUser
variable in theindex.js
looks for other one? Is that how it works? -
Evan Siroky over 8 yearsI 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 over 8 yearsOkay, 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 over 8 yearsThe naming of the variables is arbitrary and you could name it whatever when you import it with
require
. You could sayvar abcd = require('../db'),
sequelize = abcd.sequelize,
Sequelize = abcd.Sequelize
. -
bashIt over 6 yearsi faced the same issue, probably there could be the typo error. example Users.create({foo: 1}). check users is correct.