Reputation: 1399
This is my first stab at using Sequelize and I am trying get a tutorial to work.
I initially used mysql but I got an error to update to mysql2 which I did.
I get the error below.
Can anyone see what I am doing wrong?
Error:
node_modules/sequelize/lib/sequelize.js:380
this.importCache[path] = defineCall(this, DataTypes);
TypeError: defineCall is not a function
part of package.json
"dependencies": {
"body-parser": "^1.17.2",
"dotenv": "^4.0.0",
"ejs": "^2.5.7",
"express": "^4.15.3",
"express-session": "^1.15.5",
"md5": "^2.2.1",
"multer": "^1.3.0",
"mysql": "^2.14.1",
"mysql2": "^1.4.1",
"node-datetime": "^2.0.0",
"nodemailer": "^4.0.1",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"password-hash": "^1.2.2",
"random-string": "^0.2.0",
"sequelize": "^4.5.0"
}
app.js
var models = require("./models");
models.sequelize.sync().then(function() {
console.log('Nice! Database looks fine')
}).catch(function(err) {
console.log(err, "Something went wrong with the Database Update!")
});
/models/index.js
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
var config = require(path.join(__dirname, '..', 'config', 'config.json'))[env];
var sequelize = new Sequelize(config.database, config.username, config.password, config);
var db = {};
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.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;
Upvotes: 0
Views: 1874
Reputation: 1692
Every file inside of your models/
folder except for index.js
is loaded by this line.
var model = sequelize.import(path.join(__dirname, file));
What actually happens is that Sequelize loads each module individually and calls the function that is exported by each. Sequelize expects you to export a module which takes two arguments, the Sequelize object and the object of data types.
function User(sequelize, DataTypes) {
return sequelize.define('users', {
// ...
});
}
exports = module.exports = User;
If you have extra files in the models folder that do not match that format then Sequelize will not know what to do with them. What other files do you have in the models/
folder?
Upvotes: 3