node.js - 如何从管理部分在 keystone.js 中创建页面

标签 node.js model keystonejs

我是 Node.js 和 keystone 的新手。 我想创建一个名为页面的新模型,与 post 相同,这是 keystone 的默认值。

我创建的新模型的代码为

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

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

最佳答案

来自 q.exec() 回调的 results 是一个数组,您可以将其分配给 local.data.pages。因此,Jade 模板中的 data.pages 是一个数组,而 data.pages.title 将为 null

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

each page in data.pages
  p= page.title

关于node.js - 如何从管理部分在 keystone.js 中创建页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30893089/

相关文章:

node.js - 使用 Azure Mobile Apps for Node 在 Cosmos DB 上执行 SQL 查询联接

mysql - 关联 3 个 SQL 实体的建议

javascript - ExtJS 5 如何在模型中动态创建字段

node.js - Angular 应用程序部署到 Azure - 无法启动 npm

node.js - Nodejs 和 MongoDB : Unable to return value from a function

ruby-on-rails - 调用自己的 Rails 方法

javascript - MongooseJS 几乎无法使用 Keystone Location 字段

mongodb - Mongoose 在更新数组中的文档时创建新的 objectId

javascript - 有没有办法在 Keystone JS 上上传视频?

node.js - 在 AWS EC2 上部署 Angular 5 应用程序