Express Js + Sequelize:如何加载模型?

时间:2015-07-27 19:43:22

标签: node.js express sequelize.js

我正在构建我的第一个node.js应用。我使用express js和sequelize来处理Data对象。我想构建一个包含我的应用程序的所有模型的模型对象,但是这样做会出错。是什么导致了他们?

在models / User.js中:

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('User', {
        id : DataTypes.INTEGER,
        firstName : DataTypes.STRING,
        lastName : DataTypes.STRING,
        email : DataTypes.STRING,
        login : DataTypes.STRING,
        password : DataTypes.STRING,
        isAdmin : DataTypes.BOOLEAN
     });
};

在models / Project.js中:

module.exports = function(sequelize, DataTypes) {
return sequelize.define('Project', {
    id : DataTypes.INTEGER,
    projectName : DataTypes.STRING
});
};

在models / Models.js中:

var Sequelize = require('sequelize');
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/../config/config.json')[env];

// initialize database connection
var sequelize = new Sequelize(config.name, config.username, config.password);

// load models
var models = [ 'Project', 'User' ];
models.forEach(function(model) {
    console.log(__dirname + '/' + model);
    console.log(sequelize.import(__dirname + '/' + model));
    module.exports[model] = sequelize.import(__dirname + '/' + model);
});

// describe relationships
(function(m) {
    m.Project.belongsToMany(m.User, {
        through : 'UserProject'
    });
    m.User.belongsToMany(m.Project, {
        through : 'UserProject'
    });
})(module.exports);

// export connection
module.exports.sequelize = sequelize;

当我尝试运行app.js时,出现以下错误:

  

/home/julien/Developpements/nodejs/rmtool/node_modules/sequelize/lib/utils.js:51       转str.trim()。replace(/ [-_ \ s] +(。)?/ g,function(match,c){return c.toUpperCa                                                                       ^       TypeError:无法读取未定义的属性'toUpperCase'       at /home/julien/Developpements/nodejs/rmtool/node_modules/sequelize/lib/utils.js:51:75       at String.replace(native)       at Object.module.exports.camelize(/home/julien/Developpements/nodejs/rmtool/node_modules/sequelize/lib/utils.js:51:23)       at Object.module.exports.camelizeIf(/home/julien/Developpements/nodejs/rmtool/node_modules/sequelize/lib/utils.js:18:22)       在新的BelongsToMany(/home/julien/Developpements/nodejs/rmtool/node_modules/sequelize/lib/associations/belongs-to-many.js:112:56)       在Mixin.belongsToMany(/home/julien/Developpements/nodejs/rmtool/node_modules/sequelize/lib/associations/mixin.js:297:21)       at /home/julien/Developpements/nodejs/rmtool/models/Model.js:20:13       在对象。 (/home/julien/Developpements/nodejs/rmtool/models/Model.js:26:4)       在Module._compile(module.js:460:26)       在Object.Module._extensions..js(module.js:478:10)       在Module.load(module.js:355:32)       在Function.Module._load(module.js:310:12)       在Module.require(module.js:365:17)       at require(module.js:384:17)       在对象。 (/home/julien/Developpements/nodejs/rmtool/routes/login.js:9:13)       在Module._compile(module.js:460:26)

1 个答案:

答案 0 :(得分:1)

当规范形式更像是:

时,宣布关系似乎很奇怪
module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    id : DataTypes.INTEGER,
    firstName : DataTypes.STRING,
    lastName : DataTypes.STRING,
    email : DataTypes.STRING,
    login : DataTypes.STRING,
    password : DataTypes.STRING,
    isAdmin : DataTypes.BOOLEAN
 }, {
  classMethods: {
    associate: function(models) {
      User.belongsToMany(models.Project, {
        through: 'UserProject'
      });
    }
  }
 });

  return User;
};

在加载所有模型后调用associate方法以避免依赖性问题。

还可以尝试将其称为models/index.js,这样您就可以require('./models')