Error in sequelize.import: defineCall is not a function
See original GitHub issueWhat you are doing?
I just had this line in my bin/www require('../models')
this load the index.js (seen below).
The it generated an error.
File structure in the models folder: models |- index.js |- user.js
index.js:
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || 'development';
var config = require('../config/config.json')[env];
var db = {};
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
var sequelize = new Sequelize(process.env.MYSQL_DATABASE , process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
host: process.env.MYSQL_HOST || 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
}
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
user.js:
module.export = function(sequelize, DataTypes)
{
return sequelize.define('users', {
username: {type: DataTypes.STRING, primaryKey: true},
password: DataTypes.STRING,
stamm: DataTypes.STRING,
lv: DataTypes.String,
countSipplinge: DataTypes.INT,
avgAge: DataTypes.INT,
sifüName: DataTypes.STRING,
sifüEMail: DataTypes.STRING,
sifüAdresse: DataTypes.STRING,
sifüTel: DataTypes.STRING,
email: DataTypes.STRING,
admin: DataTypes.BOOLEAN,
sippe: DataTypes.BOOLEAN,
inactive: DataTypes.BOOLEAN
})
};
What do you expect to happen?
I expected to load the File and Model user.js as seen below:
What is actually happening?
But just run into an error:
C:\Users\Jan\WebstormProjects\BdPWebanwendung\node_modules\sequelize\lib\sequelize.js:691
this.importCache[path] = defineCall(this, DataTypes);
^
TypeError: defineCall is not a function
at Sequelize.import (C:\Users\Jan\WebstormProjects\BdPWebanwendung\node_modules\sequelize\lib\sequelize.js:691:30)
at C:\Users\Jan\WebstormProjects\BdPWebanwendung\models\index.js:32:33
at Array.forEach (native)
at Object.<anonymous> (C:\Users\Jan\WebstormProjects\BdPWebanwendung\models\index.js:31:4)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\Jan\WebstormProjects\BdPWebanwendung\config\passport.js:5:14)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
I checked with an debugger where it actually happens. The error is generated by the following line:
if (!this.importCache[path]) {
var defineCall = (arguments.length > 1 ? arguments[1] : require(path));
if (typeof defineCall === 'object') {
// Babel/ES6 module compatability
defineCall = defineCall.default;
}
this.importCache[path] = defineCall(this, DataTypes);
}
defineCall is undefined and it was set by the line defineCall = defineCall.default;
.
In this line is defineCall the object of require('path/to/user.js')
but defineCall.default
is undefined
.
Dialect: mysql Database version: 5.7.12 Sequelize version: 3.27.0
Issue Analytics
- State:
- Created 7 years ago
- Comments:26 (3 by maintainers)
Top Results From Across the Web
defineCall is not a function, sequelize error - Stack Overflow
The error is, as I understand it generated because I'm importing something that's not a sequelize model to my index.js file (see below...
Read more >[Solved]-defineCall is not a function, sequelize error-sequelize.js
In the model that you have specified it exports a model definition and that is the reason for error. sequelize expects every model...
Read more >Node.js – Error on sequelize.import: defineCall is not a function
The error I'm getting is this: C:\Users\brend\project\server\node_modules\sequelize\lib\sequelize.js:392 this.importCache[path] = defineCall(this, ...
Read more >defineCall is not a function at Sequelize.import sequelize.js
//BASICALLY DON'T ADD EMPTY FILES TO YOUR MODELS FOLDER /* I experienced this issue, the reason was because I added a file on...
Read more >Sequelize TypeError: defineCall is not a function - Treehouse
After doing some research, it appears that this could be caused when trying to import a non-sequelize object. Below is the problematic index.js ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That means you have tried to import a module that did not do a
module.exports = function() {
I experienced this issue, the reason was because I added a file on my models directory that is not a model, just some code that’s meant to do some stuff. I suppose we can only add model files on the models directory, as
index.js
checks every file in the whole directory.Can we edit the codes in the
index.js
to look forfilename.model.js
instead of every file or maybe make it configurable withmodel
as the default?