Javascript Sequelize获取虚拟字段
出于同步的原因,我想创建一行中某些字段的散列作为虚拟字段 我的sequelize模型如下所示:Javascript Sequelize获取虚拟字段,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,出于同步的原因,我想创建一行中某些字段的散列作为虚拟字段 我的sequelize模型如下所示: var crypto = require('crypto'); module.exports = function(sequelize, DataTypes) { return sequelize.define('transactions', { id: { type: DataTypes.INTEGER, primaryKey: true },
var crypto = require('crypto');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('transactions', {
id: {
type: DataTypes.INTEGER,
primaryKey: true
},
randomfieldone: {
type: DataTypes.BIGINT,
allowNull: true,
},
randomfieldtwo: {
type: 'NUMERIC',
allowNull: false,
},
hash: {
type: DataTypes.VIRTUAL,
set: function (val) {
var string = this.randomfieldone+''+this.randomfieldtwo;
var hash = crypto.createHash('md5');
hash.update(string);
hash.digest('hex');
this.setDataValue('hash', hash);
}
}
},{
timestamps: false
});
};
当我试图输出它时,我得到“未定义”
我希望能够像访问任何其他“真实”领域一样访问它
console.log(row.hash)
我做错了什么?好的,我解决了:
var md5 = require('MD5');
getterMethods: {
hash: function () {
var string = this.id+''+this.randomfieldone +''+this.randomfieldtwo;
var hash = md5(string);
return hash;
}
}
如果您为模式中的属性设置了
getter
函数,则当实例转换为object或json时,将包括该属性。我正在使用
hash: {
type: DataTypes.VIRTUAL,
set: function (val) {
var string = this.get("randomfieldone")+''+this.get("randomfieldtwo");
var hash = crypto.createHash('md5');
hash.update(string);
hash.digest('hex');
this.setDataValue('hash', hash);
}
}