Javascript Keystone JS-更新/种子设定/与关系
我对KeystoneJS还相当陌生,正在努力通过种子/更新预填充数据库。我对独立财产没有问题,但对有关系的财产却很挣扎 例如,我有一个包含照片的位置模型Javascript Keystone JS-更新/种子设定/与关系,javascript,node.js,mongodb,mongoose,keystonejs,Javascript,Node.js,Mongodb,Mongoose,Keystonejs,我对KeystoneJS还相当陌生,正在努力通过种子/更新预填充数据库。我对独立财产没有问题,但对有关系的财产却很挣扎 例如,我有一个包含照片的位置模型 var Location = new keystone.List('Location', { sortable: true, autokey: { path: 'slug', from: 'name', unique: true } }); Location.add({ name: {
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中预先填充两者之间的关系,并且缺乏解决问题的知识。我做了很多研究,尝试了不同的方法,比如使用\uuu ref
和\u id
,但都没能成功。我在KeystoneJS文档中也找不到答案。也许有一些明显的东西我真的错过了
exports.create = {
Location: [
{
name: 'London',
photos: [
// <-- how to do it here?
]
},
{
name: 'New York',
photos: [
// <-- how to do it here?
]
}
]
};
exports.create={
地点:[
{
名称:“伦敦”,
照片:[
//我设法通过映射照片来解决问题,并用标题中找到的实际照片来替换。下面是我如何做到的,以防它能帮助其他人:
exports = module.exports = function (next) {
Promise.all([
{
name: 'London',
photos: ['london_1', 'london_2']
},
{
name: 'New York',
photos: ['new_york_1', 'new_york_2']
}
].map(function (location) {
var _photos = location.photos || [];
location.photos = [];
return Promise.all([
_photos.map(function (title) {
return Photo.model.findOne({ title: title })
.then(function (photo) {
location.photos.push(photo);
});
})
])
.then(function () {
new Location.model(location).save();
});
}))
.then(function () {
next();
})
.catch(next);
};
在Photo
模型中,为什么不在Photo中添加一个带有type:Types.Relationship
的字段?如果您添加它,它会工作吗?只是好奇,我不确定。非常感谢您的回复。您的意思是在Photo中添加一个带有type:Types.Relationship
的新字段?Photo模型已经链接到带有的位置e> Photo.relationship({ref:'Location'
。这就是KeystoneJS文档要求将子模型链接到其父模型的方式,除非我误解了。