Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'describe' of undefined #6

Open
felipe2rod opened this issue Jun 24, 2019 · 2 comments
Open

Cannot read property 'describe' of undefined #6

felipe2rod opened this issue Jun 24, 2019 · 2 comments

Comments

@felipe2rod
Copy link

Hi! I have the following code:

const Sequelize = require('sequelize');
require('sequelize-isunique-validator')(Sequelize);
const Model = Sequelize.Model;
const sequelize = require('../config/db');
class User extends Model {}
User.init({
  // attributes
  username: {
    type: Sequelize.STRING(15),
    allowNull: false,
	isUnique: true,
    validate: {
    	isAlphanumeric: {
    		args: true,
    		msg: 'São aceitos apenas letras e números nesse campo.'
    	},
    	isUnique: sequelize.validateIsUnique('username','Este nome já está sendo utilizado!'),
    	notNull: {
    		args: true,
    		msg: 'Preencha este campo'
    	}
    }
  },
  email: {
    type: Sequelize.STRING(70),
    allowNull: false,
    unique: {
	    args: true,
	    msg: 'Este e-mail já está sendo utilizado!'
	},
    validate: {
    	isEmail: {
    		args: true,
    		msg: 'Digite um e-mail válido.'
    	},
    	notNull: {
    		args: true,
    		msg: 'Preencha este campo'
    	}
    }
  },
  password: {
  	type: Sequelize.STRING(15),
  	allowNull: false,
  	validate:{
  		len: {
	  		args: [5,10],
	  		msg: 'A senha deve possuir entre 5 e 10 caracteres.'
	  	},
	    notNull: {
	    	args: true,
	    	msg: 'Preencha este campo'
	    }
  	}
  },
  first_name: {
    type: Sequelize.STRING(15),
    allowNull: false,
    validate: {
    	is: {
    		args: ["^[a-z]+$",'i'],
    		msg: 'São aceitos apenas letras nesse campo.'
    	},
	    notNull: {
	    	args: true,
	    	msg: 'Preencha este campo'
	    }
    }
  },
  last_name: {
    type: Sequelize.STRING(15),
    validate: {
    	is:{
    		args: ["^[a-z]+$",'i'],
    		msg: 'São aceitos apenas letras nesse campo.'
    	}
    }
  },
  is_admin: {
    type: Sequelize.BOOLEAN,
    defaultValue: false
  },
 
}, {
  sequelize,
  hooks: {
	  beforeCreate: (user) => {
	    const salt = bcrypt.genSaltSync();
	    user.password = bcrypt.hashSync(user.password, salt);
	  }
	},
	instanceMethods: {
	  validPassword: function(password) {
	    return bcrypt.compareSync(password, this.password);
	  }
  }, 
  modelName: 'user'
  // options
});

function cryptPassword(password, callback) {
    bcrypt.genSalt(10, function(err, salt) { // Encrypt password using bycrpt module
        if (err)
            return callback(err);

        bcrypt.hash(password, salt, function(err, hash) {
            return callback(err, hash);
        });
    });
}

When i run a update or insert with a existing username, the return is it:

{
    "name": "SequelizeValidationError",
    "errors": [
        {
            "message": "Cannot read property 'describe' of undefined",
            "type": "Validation error",
            "path": "username",
            "value": "John1",
            "origin": "FUNCTION",
            "instance": {
                "id": null,
                "username": "John1",
                "email": "[email protected]",
                "password": "felipe",
                "first_name": "Felipe",
                "is_admin": false,
                "updatedAt": "2019-06-24T20:15:07.899Z",
                "createdAt": "2019-06-24T20:15:07.899Z"
            },
            "validatorKey": "isUnique",
            "validatorName": null,
            "validatorArgs": [],
            "original": {}
        }
    ]
}

My package.json is:

{
  "name": "timtim-backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Felipe Rodrigues",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^3.0.6",
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mysql2": "^1.6.5",
    "sequelize": "^5.8.12",
    "sequelize-isunique-validator": "^1.1.1"
  }
}
@pravin-logicap
Copy link

It does not answer but just want to know,
sequelize-isunique-validator, is it support node 10 and greater?

@AmbrosioV
Copy link

same error here, any clues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants