0

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;

1 Answer 1

3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you!!! For pointing that out, there was a stray text file, I deleted it and now the error is gone. What would I need to do if I need to have more files in the models folder? Could you give me an example?
You could create an array of files to ignore in your index.js and in the filter call you could loop over the ignore files to make sure it isn't found in the entire array. :) . const ignored = ['index.js', 'credentials.txt', 'sqlite.db']; If you use Lodash you could do something like !_.includes(ignored, file) in your filter call.
Uh, I was really struggling on this. Thanks for sharing!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.