Sequelize hasMany / belongsToMany和实例方法

时间:2015-01-19 19:09:46

标签: node.js postgresql express sequelize.js

我正在编写一个用于管理SequelizeJS(版本2.0.0-rc6)和ExpressJS的系统用户的CRUD。一个用户可以在许多用户组中,一组用户可以有许多用户。问题是,当我尝试将用户添加到一组用户时,sequelize会抛出以下错误:

Possibly unhandled TypeError: Cannot call method 'replace' of undefined
at Object.module.exports.removeTicks (/home/app/node_modules/sequelize/lib/utils.js:512:14)
at Object.module.exports.addTicks (/home/app/node_modules/sequelize/lib/utils.js:508:29)
at Object.module.exports.QueryGenerator.quoteIdentifier (/home/app/node_modules/sequelize/lib/dialects/postgres/query-generator.js:881:22)
at generateJoinQueries (/home/app/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1100:72)
at Object.<anonymous> (/home/app/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1165:29)
at Array.forEach (native)
at Object.module.exports.QueryGenerator.selectQuery (/home/app/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1164:25)
at module.exports.QueryInterface.select (/home/app/node_modules/sequelize/lib/query-interface.js:679:35)
at null.<anonymous> (/home/app/node_modules/sequelize/lib/model.js:728:34)

模型/用户group.js

module.exports = function(sequelize, DataTypes) {
   var UserGroup = sequelize.define("UserGroup", {
      id: {
         type: DataTypes.INTEGER,
         autoIncrement: true,
         primaryKey: true
      },
      name: {
         type: DataTypes.STRING(100),
         allowNull: false
      }
   }, {
      tableName: "user_group",
      classMethods: {
         associate: function(models) {
            UserGroup.hasMany(models.User, {
               through: "user_to_user_group"
            });
         }
      }
   });
   return UserGroup;
};

模型/ user.js的

module.exports = function(sequelize, DataTypes) {
   var User = sequelize.define("User", {
      id: {
         type: DataTypes.INTEGER,
         autoIncrement: true,
         primaryKey: true
      },
      first_name: {
         type: DataTypes.STRING(100),
         allowNull: false
      },
      last_name: {
         type: DataTypes.STRING(100),
         allowNull: false
      },
      cpf: DataTypes.STRING(11),
      rg: DataTypes.STRING(10),
      birth_day: DataTypes.DATE,
      password: {
         type: DataTypes.STRING(100),
         allowNull: false
      },
      active: {
         type: DataTypes.BOOLEAN,
         defaultValue: false
      }
   }, {
      tableName: "user",
      classMethods: {
         associate: function(models) {
            User.hasMany(models.Email);
            User.hasMany(models.Phone);
            User.hasMany(models.Address);
            User.belongsToMany(models.UserGroup, {
               through: "user_to_user_group"
            });
         }
      },
   });
   return User;
};

路线/ user.js的

router.post('/user/add', function(req, res, next) {

   var data = req.body;

   model.User.build(data.user)
   .save()
   .then(function(newUser){
      async.map(data.user.user_group_ids, function(userGroupId, callback){
         model.UserGroup.find({
            where: {
               id: userGroupId
            }
         })
         .then(function(userGroup) {
            // I'm getting here without any trouble
            userGroup.addUser(newUser) // this line throws the error
            .then(function(){
               callback(null, userGroup);    
             });
         });
      }, function(error, userGroups){
         if (!error) {
            res.status(200).send('User created!');
         } else {
            res.status(500).send('Something bad happened!');
         }
      });
   })
   .catch(function(error){
      console.log(error);
   });
});

0 个答案:

没有答案