我想在Postgres存储之前对图片进行编码,我在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;
}
我收到以下错误:
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)
由于某种原因,它似乎没有识别我的实例方法?!! 这看起来很奇怪,因为我有一个用户模型,它使用相同类型的钩子加密密码,它工作正常,这是用户模型进行比较:
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;
}
答案 0 :(得分:5)
您可以使用下面定义的beforeCreate
挂钩
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);
});
}
}
答案 1 :(得分:4)
实例方法仅适用于模型的实例,例如
User.create({
name : "foobar"
})
.then(function (userInstance){
userInstance.encodePoster()
});
为了能够User.encodePoster()
,您需要将encodePoster设为class method