0

var bcrypt = require('bcrypt-nodejs');
module.exports = (sequelize, DataTypes) => {
  const user = sequelize.define('user', {
    email: DataTypes.STRING,
 password: DataTypes.STRING,
 status: DataTypes.INTEGER
  }, {});
  user.associate = function(models) {
    // associations can be defined here
  };
  user.validPassword =  function (password) console.log(this.password);
};
  return user;
};

Getting this error in node js I am using "sequelize": "^5.21.3", "sequelize-cli": "^5.5.1" enter image description here

Ravi Rathore
  • 161
  • 1
  • 10

1 Answers1

0

It should be:

 user.validPassword =  function (password) { console.log(this.password) };

you are missing the curly braces.

or arrow function,

 user.validPassword =  password => console.log(this.password);

try this code:

'use strict';
var bcrypt = require('bcrypt-nodejs');

module.exports = (sequelize, DataTypes) => {
  var user = sequelize.define('user', {
    email: DataTypes.STRING,
    password: DataTypes.STRING,
    status: DataTypes.INTEGER
  }, {});
  user.associate = function (models) {

  };

  user.prototype.validPassword = function (password) {
    return bcrypt.compareSync(password, this.password);
  };
  return user;
}; 

hope this helps.

Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39
  • 'use strict'; var bcrypt = require('bcrypt-nodejs'); module.exports = (sequelize, DataTypes) => { var user = sequelize.define('user', { email: DataTypes.STRING, password: DataTypes.STRING, status: DataTypes.INTEGER }, {}); user.associate = function(models) { // associations can be defined here }; user.prototype.validPassword = function (password) { return bcrypt.compareSync(password, this.password); }; return user; }; – Ravi Rathore Jan 11 '20 at 08:50
  • updated my answer, can you save the code as a file and run it ? – Arun Kamalanathan Jan 11 '20 at 08:57
  • `'use strict'; var bcrypt = require('bcrypt-nodejs'); module.exports = (sequelize, DataTypes) => { var user = sequelize.define('user', { email: DataTypes.STRING, password: DataTypes.STRING, status: DataTypes.INTEGER }, {}); user.associate = function(models) { // associations can be defined here }; user.prototype.validPassword = function (password) { return bcrypt.compareSync(password, this.password); }; return user; };` – Ravi Rathore Jan 11 '20 at 09:00
  • [https://prnt.sc/qm687o] please open this image. in this image i have define the passport module. – Ravi Rathore Jan 11 '20 at 09:04
  • I am not following you, can you create a GitHub gist and share me the file. Just show me the minimal reproduce-able code, because I ran this code and I didn't have any issues. – Arun Kamalanathan Jan 11 '20 at 09:58
  • Please check this below url: https://github.com/sequelize/sequelize/issues/7632 User name : ravirathoreds – Ravi Rathore Jan 13 '20 at 05:42