续集获取虚拟领域
出于同步的原因,我想创build一个行的某些领域的散列作为一个虚拟的领域。
我的续集模型看起来像这样:
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)
我在这里做错了什么?
我正在使用
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); } }
好吧,我解决了它:
var md5 = require('MD5'); getterMethods: { hash: function () { var string = this.id+''+this.randomfieldone +''+this.randomfieldtwo; var hash = md5(string); return hash; } }
如果您为模式中的属性设置getter
函数,那么当实例转换为object或json时,将包含该属性。