无法在 beforeCreate 挂钩中设置日期 - Strapi (beta14)

Cannot set date in beforeCreate hook - Strapi (beta14)

创建了新闻模型:News.js

beforeCreate: async (model, attrs) => {
  if (attrs['Date'] === null) {
    model.set('Date', '2030-00-00 00:00:00+00');
  }
}

当我创建 post 时,如果用户没有填写日期字段,我希望它自动预填写。但是 model.set() 似乎在我创建新页面时没有效果。

我建议你使用beforeSave函数。

beforeSave: async (model, attrs, options) => {
  // detect if it's a creation or an update
  if (options.method === 'insert') {
    // statement for creation
    if (attrs['Date'] === null) {
      model.set('Date', '2030-00-00 00:00:00+00');
    }
  }
}

不是直接改变模型,而是改变 attrs.

beforeCreate: async (model, attrs) => {
  if (attrs['Date'] === null) {
    attrs['Date'] = '2030-00-00 00:00:00+00';
  }
}