Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Instance Methods in Sequelize

Tags:

Can someone help me understand how to use instance methods in Sequelize? I've reviewed the documentation but have found it to be sparse. At present, I am trying to use setPassword and verifyPassword instance methods on my user model. When I try to call the code in the REPL, after having imported the user model and synced the DB, I get the following:

> models.User.setPassword('test');
TypeError: Object [object Object] has no method 'setPassword'

Here is the code for the user model:

var bcrypt = require('bcrypt');

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('User', {
    email: { type: DataTypes.STRING, unique: true, allowNull: false, validate: { isEmail: true } },
    password: { type: DataTypes.STRING, allowNull: false},
    firstName: {type: DataTypes.STRING},
    lastName: {type: DataTypes.STRING},
    companyName: {type: DataTypes.STRING},
    admin: {type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false,},
    forgotUrl: {type: DataTypes.STRING, unique: true},
    forgotDate: {type: DataTypes.STRING},
    lastLogin: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW
    }
  }, {
    paranoid: true,
    instanceMethods: {
      setPassword: function(password, done) {
        return bcrypt.genSalt(10, function(err, salt) {
          return bcrypt.hash(password, salt, function(error, encrypted) {
            this.password = encrypted;
            this.salt = salt;
            return done();
          });
        });
      },
      verifyPassword: function(password, done) {
        return bcrypt.compare(password, this.password, function(err, res) {
          return done(err, res);
        });
      }
    }
  });
};
like image 803
surfearth Avatar asked Oct 17 '13 17:10

surfearth


People also ask

What is class method Sequelize?

Sequelize allows you to add class-level methods to any defined models in your JavaScript code. Usually, class-level methods are reserved for code that modifies the table, while instance methods are used for code that modifies your table rows.

What is Upsert Sequelize?

upsert() returns a boolean indicating whether the row was created or updated.

How do I insert data into a table using Sequelize?

The create() method is used to insert a single row into your SQL table. When you need to insert multiple rows at once, you need to use the bulkCreate() method instead. Finally, you can also write and execute a raw SQL statement using the sequelize. raw() method.

How do you define an object in Sequelize?

You can use JSON datatype of field in Sequelize (this corresponds to JSON datatype in PostgreSQL). const User = sequelize. define('User', { name: { type: DataTypes. STRING, allowNull: false }, age: { type: DataTypes.


2 Answers

Instance method can be used on specific element instances eg.

models.User.find(123).success( function( user ) { 
    user.setPassword('test');
});
like image 128
SergeS Avatar answered Sep 19 '22 23:09

SergeS


You define the function as: function(password, done)

Yet you don't supply the done parameter. Thus, the function leaves done as undefined and calling done() is executing an undefined function.

You could fix this in 3 ways:

  1. Default done to a noop function function () {}
  2. Only return done() if done is defined
  3. Supply a done callback when calling the instance function.

The alternative is to refactor it to return a promise which it resolves on completion.

like image 37
Pyro Avatar answered Sep 20 '22 23:09

Pyro