Sequelizejs没有显示所有验证错误或消息

时间:2013-06-11 09:03:16

标签: node.js sequelize.js

这是我的models.js

var User = require('../../managedb').User;

module.exports = function(sequelize, DataTypes) {


  return sequelize.define('User', {
    username: {
      type: DataTypes.STRING,
      unique: {
        msg: "Username already exists!"
      }
    },
    password: {
      type: DataTypes.STRING,
      len: {
        args: 6,
        msg: "Password must be atleast 6 characters in length"
      }
    },
    email_id:{
      type: DataTypes.STRING,
      unique:{
        msg: "Email already exists!"
      },
      validate: {
        isEmail: {
          msg: "Email is invalid!"
        },
        notNull: true,
      }
    },
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    }
  },
    {
    instanceMethods: {
      countTasks: function() {
        // how to implement this method ?
      }
    }
  });

这是我尝试使用非唯一用户名和电子邮件保存对象时收到的错误响应:

'{"length":173,"name":"error","severity":"ERROR","code":"23505","detail":"Key (email_id)=(yup2@gmail.com) already exists.","file":"nbtinsert.c","line":"397","routine":"_bt_check_unique"}'

我首先要运行validate()函数吗?我在哪里错了?

1 个答案:

答案 0 :(得分:3)

验证必须是属性定义中validate对象的一部分:

return sequelize.define('User', {
  username: {
    type: DataTypes.STRING,
    validate: {
      unique: {
        msg: "Username already exists!"
      }
    }
  }
})