[英]Sequelize TypeError: defineCall is not a function
Working on an express project with a SQLite database.使用 SQLite 数据库处理 express 项目。
I'm getting an Sequelize TypeError that I've been working on for hours but I'm coming up against a brick wall:我遇到了一个 Sequelize TypeError,我已经研究了几个小时,但我遇到了砖墙:
C:\-----\node_modules\sequelize\lib\sequelize.js:392
this.importCache[path] = defineCall(this, DataTypes);
^
TypeError: defineCall is not a function
at Sequelize.import (C:\----\node_modules\sequelize\lib\sequelize.js:392:32)
at C:\----\models\index.js:25:32
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 file.
下面是有问题的 index.js 文件。
index.js:索引.js:
var Sequelize = require('sequelize');
var config = {
dialect: 'sqlite',
storage: 'library.db'
};
var connection = new Sequelize(config);
var contents = fs.readdirSync(__dirname);
var db = {};
contents = contents.filter(function(file){
var currFileName = __filename.split('/');
var checkOne = file.substr(file.length - 3, 3) === '.js';
var checkTwo = file !== currFileName[currFileName.length - 1];
return checkOne && checkTwo;
});
contents.forEach(function(file){
var path = [__dirname, file].join('/');
var model = connection.import(path);
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName){
var model = db[modelName];
if(model.associate) {
model.associate(db);
}
});
module.exports = {
models: db,
connection: connection
};
I do not have any function called defineCall, any idea where the error is coming from?我没有任何称为defineCall的函数,知道错误来自哪里吗?
This is indeed caused by importing a file that's not a Sequelize model.这确实是由于导入了不是 Sequelize 模型的文件引起的。 In my case, it was because my
index.js
was pulling in my test files as well as the models, which were in the same directory.就我而言,这是因为我的
index.js
正在拉入我的测试文件以及位于同一目录中的模型。 Try adding another check like checkOne
to exclude anything that ends with .test.js
.尝试添加另一个检查,如
checkOne
以排除以.test.js
结尾的任何.test.js
。
With the hint from the answer by @Floppy, I got to realise that it would be better if we stored those related files encapsulated in a folder.根据@Floppy 的回答的提示,我意识到如果我们将这些相关文件存储在一个文件夹中会更好。
For eg.例如。 make a folder named
Models
and store your index.js
with all the models (eg. User model, photos model, etc) and then try.创建一个名为
Models
的文件夹并将您的index.js
与所有模型(例如用户模型、照片模型等)一起存储,然后尝试。
Thanks.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.