【发布时间】:2018-04-13 14:26:41
【问题描述】:
我对 KeystoneJS 还很陌生,并且很难通过种子/更新来预填充数据库。我对独立属性没有任何问题,但对具有关系的属性感到困惑。
例如,我有一个包含照片的位置模型。
var Location = new keystone.List('Location', {
sortable: true,
autokey: {
path: 'slug',
from: 'name',
unique: true
}
});
Location.add({
name: {
type: Types.Text,
required: true,
initial: true
},
photos: {
type: Types.Relationship,
ref: 'Photo',
many: true
}
}
照片模型是这样的:
var Photo = new keystone.List('Photo', {
autokey: {
path: 'slug',
from: 'title',
unique: true
}
});
Photo.add({
title: {
type: Types.Text,
initial: true,
index: true
},
image: {
type: Types.CloudinaryImage,
required: true,
initial: false
}
});
Photo.relationship({
ref: 'Location',
path: 'photos',
refPath: 'photos'
});
在更新文件夹中,我尝试使用预加载的数据为数据库播种。位置和照片模型都单独填充,但我未能在管理 UI 中预先填充两者之间的关系,并且缺乏有关如何解决的知识。我做了一些研究,尝试了不同的方法,例如使用 __ref 和 _ids,但无法使其工作。我也无法在 KeystoneJS 文档中找到答案。也许有一些明显的东西我实际上错过了。
exports.create = {
Location: [
{
name: 'London',
photos: [
// <-- how to do it here?
]
},
{
name: 'New York',
photos: [
// <-- how to do it here?
]
}
]
};
有人知道预填充 KeystoneJs 数据库关系的正确方法吗?非常感谢。
【问题讨论】:
-
在
Photo模型中,为什么没有type: Types.Relationship的字段?如果添加它,它会起作用吗?只是好奇,我不确定。 -
非常感谢您的回复。你的意思是在照片中添加一个带有
type: Types.Relationship的新字段?照片模型已通过Photo.relationship({ ref: 'Location'链接到位置。这就是 KeystoneJS 文档要求将子模型链接到其父模型的方式,除非我误解了。
标签: javascript node.js mongodb mongoose keystonejs