Sequelize错误:在Index.js中未定义defineCall

时间:2016-07-06 06:50:01

标签: mysql node.js express sequelize.js

我一直在收到这个续集错误

/node_modules/sequelize/lib/sequelize.js:508
      this.importCache[path] = defineCall(this, DataTypes);
                           ^
    TypeError: defineCall is not a function
    at module.exports.Sequelize.import             (/node_modules/sequelize/lib/sequelize.js:508:32)
at   /models/Index.js:16:33
at Array.forEach (native)
at Object.<anonymous> (/Users/vincentporta/Desktop/RCB_Classwork/cesarcell/models/Index.js:15: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)

我已将其缩小到我下面的Index.js文件中的注释代码

"use strict";

var fs        = require("fs");
var path      = require("path");
var Sequelize = require("sequelize");
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;

有谁知道为什么会出现这个错误?

4 个答案:

答案 0 :(得分:8)

这通常意味着您正在尝试导入非续集对象。如果索引文件的名称是Index.js,那么您需要在过滤器函数中更改它(首字母是大写)

return (file.indexOf(".") !== 0) && (file !== "Index.js");

因为它区分大小写。还要检查导入文件夹中没有任何其他文件没有定义sequelize模型。如果只是将它们添加到过滤器功能中。

答案 1 :(得分:7)

我刚才看到了类似的问题,问题是模型文件夹中实际上不是数据库模型的文件。确保models文件夹仅包含数据库的模型。希望这有帮助!

答案 2 :(得分:0)

如果您也是循环导入,也可能发生这种情况。

答案 3 :(得分:0)

就我而言,它丢失了default

此代码导致了错误:

@Table
export class User extends Model<User> {
  @Column
  userId!: number;

  @Column
  tenantId!: number;
}

default在类定义中对其进行了修复:

@Table
export default class User extends Model<User> {
  @Column
  userId!: number;

  @Column
  tenantId!: number;
}