【问题标题】:Sequelize TypeError: defineCall is not a functionSequelize TypeError:defineCall 不是函数
【发布时间】:2017-12-17 22:24:23
【问题描述】:

使用 SQLite 数据库进行快速项目。

我遇到了一个 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

经过一些研究,这似乎是在尝试导入非续集对象时引起的。下面是有问题的 index.js 文件。

index.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
};

我没有任何名为defineCall的函数,知道错误来自哪里吗?

【问题讨论】:

    标签: javascript sequelize.js sequelize-cli


    【解决方案1】:

    这确实是由于导入了不是 Sequelize 模型的文件造成的。就我而言,这是因为我的index.js 正在拉入我的测试文件以及模型,它们位于同一目录中。尝试添加另一个检查,例如 checkOne,以排除以 .test.js 结尾的任何内容。

    【讨论】:

      【解决方案2】:

      在@Floppy 回答的提示下,我意识到如果我们将这些相关文件封装在一个文件夹中会更好。

      例如。创建一个名为Models 的文件夹并将您的index.js 与所有模型(例如用户模型、照片模型等)一起存储,然后尝试。

      谢谢。

      【讨论】:

        猜你喜欢
        • 2021-12-04
        • 2020-04-08
        • 2018-07-30
        • 2017-04-24
        • 2019-11-17
        • 2021-07-27
        • 1970-01-01
        • 2020-12-01
        • 2020-02-04
        相关资源
        最近更新 更多