在KeystoneJS中,如何使用many:true在关系字段中初始选择所有值?

时间:2017-07-18 17:17:06

标签: keystonejs

我正在使用KeystoneJS。我的Post模型中有一个关系字段,它引用了另一个名为Device的模型。我使用很多:true允许用户从Device模型中选择多个项目。

有没有办法在创建帖子时最初选择它们?

现在我的关系字段看起来像这样:

availableDevices: {
  type: Types.Relationship,
  ref: 'Device',
  many: true
}

1 个答案:

答案 0 :(得分:0)

在模型定义中,您可以添加预处理步骤以填充您希望在关系中具有默认值的字段。我没有用Report.schema.pre('save', function (next) { this.wasNew = this.isNew; if (!this.employee) { this.employee = this.createdBy; } if (!this.timeEnteredTheHomeStr) { if(!this.timeEnteredTheHome) { this.timeEnteredTheHome = Date.now } this.timeEnteredTheHomeStr = String(this.timeEnteredTheHome); } next(); }); 关系做到这一点,但我已经用这样的关系完成了这个:

employee

当有人创建新的报表对象时,createdBy字段会自动设置为{{1}}字段中引用的用户对象。