2

我是 node.js 和 keystone 的新手。我想创建一个名为 pages 的新模型,它与 keystone 默认的 post 相同。

我创建的新模型代码为

var pages = new keystone.List('pages', {
    map: { name: 'title' },
    autokey: { path: 'slug', from: 'title', unique: true }
});

pages.add({
    title: { type: String, required: true },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'published', index: true },
    author: { type: Types.Relationship, ref: 'User', index: true },
    publishedDate: { type: Types.Date, index: true, dependsOn: { state: 'published' } },
    image: { type: Types.LocalFile, dest: 'public/uploads'},
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 }
    },
});

pages.schema.virtual('content.full').get(function() {
    return this.content.extended || this.content.brief;
});

pages.defaultColumns = 'title, state|20%, author|20%, publishedDate|20%';
pages.register();

然后我为页面创建了一条路线

exports = module.exports = function(req, res) {

    var view = new keystone.View(req, res),
        locals = res.locals;

    // Init locals
    locals.section = 'pages';
    locals.data = {
        pages: []
    };

    // Load the pages
    view.on('init', function(next) {

        var q = keystone.list('pages').paginate({
                page: req.query.page || 1,
                perPage: 10,
                maxPages: 10
            })
            .where('state', 'published')
            .sort('-publishedDate');
        q.exec(function(err, results) {
            locals.data.pages = results;
            next(err);
        });

    });

    // Render the view
    view.render('pages');
};

还创建了中间件

{ label: 'pages',       key: 'pages',       href: '/pages' }

这就是我为创建模型所做的一切......让我知道我错过了什么。当我通过以下代码从 pages.jade 文件中调用它时:

data.pages.title

这告诉我什么。即使没有显示任何错误。任何帮助将不胜感激。

4

1 回答 1

2

results回调中的q.exec()是一个数组,您将其分配给local.data.pages. 因此data.pages,在您的 Jade 模板中是一个数组,并且data.pages.titlenull.

您需要遍历返回的页面。下面的示例将呈现页面标题,假设返回的结果数组不为空。

each page in data.pages
  p= page.title
于 2015-06-17T22:57:27.717 回答