KeystoneJS 关系字段不保存空值

KeystoneJS Relationship field not saving empty value

KeystoneJS 中的常规 Relationship 字段(本例中为 categories):

Post.add({
    author: { type: Types.Relationship, ref: 'User' },
    categories: { type: Types.Relationship, ref: 'PostCategory', many: true }
});

最初它是空的,但是当我向它添加一些值时,保存它然后清除它(这样该字段再次为空),保存这个模型不会更新 categories.

console.log(this.categories) in Post.schema.pre('save', ... 显示 this.categories 有以前的值,而 它应该是空的 .

对这种行为有什么想法吗?

显然这是 KeystoneJS 的 "feature"。

Here 如果 value === undefined 他们会忽略更新,不幸的是,当您有一个值并将其删除时就是这种情况。

解决方案:我将 undefined 检查下移到 this line,所以它看起来像这样:

else if ((value === undefined || !value) && item.get(this.path)) { ... }