[英]Sequelize.js setter function not working as I expect
I'm having trouble using the setter functions on my Sequelize js model properties. 我在Sequelize js模型属性上使用setter函数时遇到麻烦。
What I want to do is when one property is given a value use that value to populate another property in the table at the same time. 我想做的是,当给一个属性赋值时,使用该值同时填充表中的另一个属性。
In my model this is the code I have for the property in question... 在我的模型中,这是我所讨论属性的代码...
date_time_paid: {
type:DataTypes.DATE,
set(val) {
const date = new Date(val);
this.setDataValue('date_time_paid', val); // set the the current property
this.setDataValue('month_paid', `${date.getFullYear()}-${(date.getMonth() + 1)}`); //also set another property on the same row
},
},
What I expect to happen is for the date_time_paid
column to store the raw value and the month_paid
column to store a string derived from it. 我希望发生的事情是,
date_time_paid
列存储原始值, month_paid
列存储从其派生的字符串。 But when I run my seeder to populate the table with test data the value for the month_paid column remains null. 但是,当我运行播种机以用测试数据填充表时,month_paid列的值仍然为空。
I think you need a just a getter. 我认为您需要一个吸气剂。 Forget about
date_time_paid
and make getter for month_paid
: 忘掉
date_time_paid
,让getter成为month_paid
:
get month_paid(){
const date = new Date(this.date_time_paid);
return `${date.getFullYear()}-${(date.getMonth() + 1)}`;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.