简体   繁体   English

Sequelize TypeError:defineCall 不是函数

[英]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.

相关问题 defineCall不是函数,导致错误 - defineCall is not a function, sequelize error 类型错误:defineCall 不是 function。 要求()失败 - TypeError: defineCall is not a function. require() fails 续集错误 - “TypeError: hooks.concat is not a function” - Sequelize error - "TypeError: hooks.concat is not a function" Sequelize:TypeError:User.hasMany不是函数 - Sequelize: TypeError: User.hasMany is not a function 续集,Javascript TypeError: group.addTask is not a function - Sequelize, Javascript TypeError: group.addTask is not a function 使用Jasmine Sequelize TypeError进行测试:_models2.default.count不是函数 - Testing with Jasmine Sequelize TypeError: _models2.default.count is not a function Model sequelize nodejs TypeError: (intermediate value).init is not a function - Model sequelize nodejs TypeError: (intermediate value).init is not a function const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes) - TypeError: require(...) is not a function - const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes) - TypeError: require(...) is not a function TypeError:使用Sequelize在NodeJs中定义模型时,object不是函数 - TypeError: object is not a function when defining models in NodeJs using Sequelize UnhandledPromiseRejectionWarning: TypeError: user.destroy is not a function (NodeJS- Sequelize) - UnhandledPromiseRejectionWarning: TypeError: user.destroy is not a function (NodeJS- Sequelize)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM