这是我们在Keystone中设置关系的方式,如下所示。假设我有页面和二级页面模式,然后有一个脚本将获取页面数据。 猫鼬和基石是可能的。如果我查询页面列表,那么我还将获得相关的二级页面数据 从下面的代码中导致的原因是,当我获取页面列表时,它仅包含相关第二级页面的ID数组。我想得到 与单个“响应”中的页面相关的所有第二级页面(locals.data.toplevelpages)。
Pages.add({
name: { type: String, required: true },
key: { type: String, required: false },
link_destination : { type: Types.Url, require: true},
second_level_page: { type: Types.Relationship, ref: 'SecondLevelPage', many: true },
publishedDate: { type: Types.Date, index: true, dependsOn: { state: 'published' } },
state: { type: Types.Select, options: 'draft, published, archived', default: 'draft', index: true },
});
SecondLevelPage.add({
title: { type: String, required: true },
slug: { type: String, index: true },
state: { type: Types.Select, options: 'draft, published, archived', default: 'draft', index: true },
author: { type: Types.Relationship, ref: 'User', index: true },
publishedDate: { type: Types.Date, index: true },
content: {
html: { type: Types.Html, wysiwyg: true, height: 600 }
},
});
view.on('init', function (next) {
var q = keystone.list('Pages').paginate({
page: req.query.page || 1,
perPage: 10,
maxPages: 10,
filters: {
state: 'published',
},
})
.sort('publishedDate')
q.exec(function (err, results) {
locals.data.pages = results;
console.log("Response data :" , locals.data.pages)
next(err);
});
});
{ total: 8,
results:
[ { _id: 5d26e5bd30656a14a40d7594,
key: 'home',
name: 'New',
__v: 1,
link_destination: '/',
publishedDate: 2019-07-10T16:00:00.000Z,
state: 'published',
second_level_page: [Array] }, // contains ids from second level
]
}