Sequelize instance methods beforeCreate Hooks

I am trying to encode an image before storing in Postgres, I have defined my model as such in Sequelize:

var request = require('request');
module.exports = function (sequelize, DataTypes) {
    var Show = sequelize.define('Show',{
        name: DataTypes.STRING,
        genre: DataTypes.ARRAY(DataTypes.STRING),
        status: DataTypes.STRING,
        poster: DataTypes.TEXT
    }, {
        associate: function (models) {
            Show.hasMany(models.Episode)
            .hasMany(models.User)
        }
    }, {
        instanceMethods: {
            encodePoster : function (poster, done) {
                var url = 'http://thetvdb.com/banners/' + poster;
                request({url: url, encoding: null}, function (err, response, body) {
                    encodedpost = 'data:'+response.headers['content-type']+';base64,' + body.toString('base64');
                    done(err, encodedpost);
                });
            }
        }
    })
    Show.beforeCreate(function (model, done) {
        model.encodePoster(model.poster, function (err, encoded) {
            if (err) return done(err);
            model.poster = encoded;
            done();
        })
    })
    return Show;
}

      

I am getting the following error:

c:\Users\London\Apps\showtrackr\models\show.js:37
                model.encodePoster(model.poster, function (err, encoded) {
                      ^
TypeError: Object [object Object] has no method 'encodePoster'
    at null.<anonymous> (c:\Users\London\Apps\showtrackr\models\show.js:37:9)
    at method (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\hooks.js:80:8)
    at run (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\hooks.js:53:10)
    at Hooks.runHooks (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\hooks.js:65:3)
    at null.<anonymous> (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\dao.js:390:24)
    at emit (events.js:95:17)
    at module.exports.CustomEventEmitter.emit (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\emitters\custom-event-emitter.js:61:33)
    at null.<anonymous> (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\dao-validator.js:123:17)
    at Hooks.runHooks (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\hooks.js:41:15)
    at null.<anonymous> (c:\Users\London\Apps\showtrackr\node_modules\Sequelize\lib\dao-validator.js:118:29)

      

for some reason it doesn't seem to recognize my instance method? !! Seems strange, since I have a User model that uses the same hook to encrypt the password and it works great, here is the User model for comparison:

var bcrypt = require('bcrypt-nodejs');
module.exports = function (sequelize, DataTypes) {
    var User = sequelize.define('User',{
        email: DataTypes.STRING,
        password: DataTypes.STRING 
    },{
        associate: function (models) {
            User.hasMany(models.Show)
        }
    },{
        instanceMethods: {
            generateHash : function (password, done) {
                bcrypt.genSalt(10, function (err, salt) {
                    bcrypt.hash(password, salt, null, done); 
                });
            },
            validPassword : function (password, next) {
                bcrypt.compare(password, this.password, next)
            }
        }
    })
    User.beforeCreate(function (model, done) {
        model.generateHash(model.password, function (err, encrypted) {
            if (err) return done(err);
            model.password = encrypted;
            done();
        })
    })
    return User;
}

      

+3


source to share


2 answers


You can use the hook beforeCreate

as below



hooks: {
  beforeCreate:function(show, options, cb) {
    var url = 'http://thetvdb.com/banners/' + show.poster;
     request({url: url, encoding: null}, function (err, response, body) {
      if (err) return cb(err);
      encodedpost = 'data:'+response.headers['content-type']+';base64,' + body.toString('base64');
      show.poster = encodedpost;
      cb(null, options);
    });
  }
}

      

+5


source


Instance methods are only available on a model instance eg.

User.create({
    name : "foobar"
})
.then(function (userInstance){
    userInstance.encodePoster()
});

      



To do User.encodePoster()

you need to do encodePoster aclass method

+4


source







All Articles