【问题标题】:Sequelize object is not a functionSequelize 对象不是函数
【发布时间】:2015-10-26 11:37:12
【问题描述】:

我正在使用 repo 并尝试将其转换为 PostgreSQL,这是我的错误:

/home/otis/Developer/Shipwrecked/Hatchway/node_modules/sequelize/lib/sequelize.js:601
    this.importCache[path] = defineCall(this, DataTypes);
                             ^
TypeError: object is not a function
    at Sequelize.import (/home/otis/Developer/Shipwrecked/Hatchway/node_modules/sequelize/lib/sequelize.js:601:30)
    at db.sequelize (/home/otis/Developer/Shipwrecked/Hatchway/models/index.js:15:37)
    at Array.forEach (native)
    at Object.<anonymous> (/home/otis/Developer/Shipwrecked/Hatchway/models/index.js:14:6)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/otis/Developer/Shipwrecked/Hatchway/app.js:10:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/otis/Developer/Shipwrecked/Hatchway/bin/www:7:11)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

这是导致错误的代码(models/index.js):

var fs        = require("fs");
var path      = require("path");
var Sequelize = require("sequelize");
var env       = process.env.NODE_ENV || "development";
var config    = require(__dirname + '/../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;

如果我注释掉以下几行,错误就会停止:

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;
    });

Node.js 错误不是我的强项,所以我不确定发生了什么,如果需要,我也可以发布我的config.json

【问题讨论】:

  • 粘贴你的models/index.js文件
  • 中间的sn-p是index.js

标签: node.js postgresql express sequelize.js


【解决方案1】:

如果我们查看code imports

 if (!this.importCache[path]) {
    var defineCall = (arguments.length > 1 ? arguments[1] : require(path));
    this.importCache[path] = defineCall(this, DataTypes);
  }

...可以理解,它看起来像一个包含模型的文件夹,其中包含一个不是有效模型文件且通常是正确模块的文件。因此,试图通过“理想的”获取错误来激活它。检查文件夹模型。

【讨论】:

  • 干杯,在我看到你的答案之前就想通了 :)
猜你喜欢
  • 2021-12-04
  • 2020-04-08
  • 2018-07-30
  • 2017-04-24
  • 2019-11-17
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
相关资源
最近更新 更多