0

我在我的 Sequelize js 模型属性上使用 setter 函数时遇到问题。

我想要做的是,当一个属性被赋予一个值时,使用该值同时填充表中的另一个属性。

在我的模型中,这是我有问题的属性的代码......

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
  },
},

我期望发生的是date_time_paid存储原始值的month_paid列和存储从它派生的字符串的列。但是,当我运行播种机以使用测试数据填充表时,month_paid 列的值仍然为空。

  1. 这是setter函数的正确使用吗?
  2. 如果是我做错了什么,这意味着它不起作用吗?
4

1 回答 1

0

我认为你需要一个吸气剂。忘记date_time_paid并为month_paid

get month_paid(){
    const date = new Date(this.date_time_paid);
    return `${date.getFullYear()}-${(date.getMonth() + 1)}`;
}
于 2018-07-05T10:05:51.423 回答