我正在使用KeystoneJS。我的Post模型中有一个关系字段,它引用了另一个名为Device的模型。我使用很多:true允许用户从Device模型中选择多个项目。
有没有办法在创建帖子时最初选择它们?
现在我的关系字段看起来像这样:
availableDevices: {
type: Types.Relationship,
ref: 'Device',
many: true
}
答案 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}}字段中引用的用户对象。