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

Instance and Class Methods not Defined #4170

Closed
donaldhook opened this issue Jul 22, 2015 · 2 comments
Closed

Instance and Class Methods not Defined #4170

donaldhook opened this issue Jul 22, 2015 · 2 comments

Comments

@donaldhook
Copy link

I am trying to define an instance method and class method and am receiving a TypeError indicating that the methods are not defined. I am not sure what I have defined incorrectly.

Any insight is appreciated. Great library by the way!!

Below is the model definition and invocation. The error I get is

** Error Getting User: TypeError: undefined is not a function

When i try to call if (!user.authenticate(password)) ....

Here is the definition:

/* jshint indent: 2 */
var crypto = require('crypto');

module.exports = function(sequelize, DataTypes) {

var user = sequelize.define('user', {
    user_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      field: 'user_id',
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '0',
      references: { model: "company", key: "company_Id" }
      },
    role_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '0',
      references: { model: "role", key: "role_Id" }
    },
    first_name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    last_name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    username: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    salt: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    temp: {
      type: 'CHAR(1)',
      allowNull: false,
      defaultValue: 'Y'
    },
    provider: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    reset_password_token: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    reset_password_expires: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    updated: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_by: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
    },
    created: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: DataTypes.NOW
    }
  }, {
    updatedAt: 'updated',
    createdAt: 'created',
    tableName: 'user',          // this will define the table's name
    timestamps: false           // this will deactivate the timestamp columns
  }, {
      classMethods: {
          hashPassword: function (password) {
              console.log('** called hashpassword');
              if (this.salt && password.length > 6) {
                  //var salt = new Buffer(crypto.randomBytes(16).toString('base64'),'base64');
                  return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');
              } else {
                  return password;
              }
          }
      },
        instanceMethods: {
          authenticate : function (password) {
              return this.password === this.hashPassword(password);
          }
        }
      }
);

user.beforeCreate(function (model, done) {
    console.log('** Starting to hash password');
    if (model.password && model.password.length > 6){
        model.salt = new Buffer(crypto.randomBytes(16).toString('base64'),'base64');
        model.password = crypto.pbkdf2Sync(model.password,model.salt,10000,64).toString('base64');
        console.log('** Hashed Password ' + model.password + ' salt' + model.salt);
    };
});

return user;

};

Here is where I am calling from:
I added a var test = ... to see if the class methods were defined as well ...

User.find({
where: { username: username}
})
.then(function(user) {

            if (!user) {
                return done(null, false, {
                    message: 'Unknown user or invalid password'
                });
            }
            var test = User.hashPassword(password);
            // Call the Class Method - notice capital User vs lower case user
            if (!user.authenticate(password)) {
                return done(null, false, {
                    message: 'Unknown user or invalid password'
                });
            }

            console.log('** Got User ' + user.first_name + " " + user._name);
            return done(null, user);

        }).catch(function(err) {
            console.log('** Error Getting User: ' + err);
            return done(err);
        });
@janmeier
Copy link
Member

You are passing the instance and class methods in a third object - they should inside the same options object as tableName, timestamps etc.

{
    updatedAt: 'updated',
    createdAt: 'created',
    tableName: 'user',          // this will define the table's name
    timestamps: false           // this will deactivate the timestamp columns
  }, { <--- BEGONE!
      classMethods: {

@donaldhook
Copy link
Author

Ahhh... Thank you!!!

On Wed, Jul 22, 2015 at 8:02 AM, Jan Aagaard Meier <notifications@github.com

wrote:

Closed #4170 #4170.


Reply to this email directly or view it on GitHub
#4170 (comment).

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

2 participants