#node.js #apostrophe-cms
#node.js #apostrophe-cms
Вопрос:
Я пытаюсь отобразить дочерние элементы страниц с апострофом в моем объекте навигации — однако массив _children всегда пуст. На моей странице есть дочерние страницы, настроенные через пользовательский интерфейс front end Pages.
Мой index.js для модуля lib/modules/apostrophe-pages содержит следующее:
construct: function(self,options) {
// store the superclass method and call at the end
var superPageBeforeSend = self.pageBeforeSend;
self.pageBeforeSend = function(req, callback) {
// Query all pages with top_menu setting = true and add to menu collection
self.apos.pages.find(req, { top_menu: true }, {slug: 1, type: 1, _id: 1, title: 1})
.children(true)
.toArray(
function (err, docs) {
if (err) {
return callback(err);
}
req.data.navpages = docs;
return superPageBeforeSend(req, callback);
});
};
},
...
Мой атрибут top_menu устанавливается через apostrophe-custom-pages:
module.exports = {
beforeConstruct: function(self, options) {
options.addFields = [
{
name: 'subtitle',
label: 'Subtitle',
type: 'string'
},
{
name: 'css_class',
label: 'CSS Class',
type: 'string'
},
{
name: 'top_menu',
label: 'Include In Top Menu',
type: 'boolean'
}
].concat(options.addFields || []);
}
};
Это дает мне нужные страницы с настройкой top_menu.. но я тоже хочу получить дочерние страницы..
При отладке кода я вижу, что документы.массив _children присутствует, но всегда пуст, даже если на странице есть дочерние страницы…
Я попытался добавить следующие оба в мой app.js и к моему index.js но это не меняет результат:
filters: {
// Grab our ancestor pages, with two levels of subpages
ancestors: {
children: {
depth: 2
}
},
// We usually want children of the current page, too
children: true
}
Как я могу заставить мой запрос find () фактически включать дочерние страницы?
Ответ №1:
Решил это..
Мне нужно было добавить 'rank: 1, path: 1, level: 1'
к проекции согласно этой странице в документации: https://apostrophecms.org/docs/tutorials/howtos/children-and-joins.html#projections-and-children