Keystone js findOne返回null

时间:2017-09-02 02:25:49

标签: node.js keystonejs

我正在使用KeystoneJS版本4.0.0-beta.5,我试图从我的一个模型加载一些数据,由于某种原因返回null。我可以加载我所有其他模型的数据,但这个有点琐事或我忽略了一些东西。任何和所有的帮助将不胜感激。

节点版本:6.11.2

型号代码

var keystone = require('keystone');
var Types = keystone.Field.Types;

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

Event.add({
    title: {
        type: String,
        required: true
    },
    info: {
        type: Types.Html,
        wysiwyg: true,
        height: 300
    },
  form: {
    type: Types.Boolean
  }
});

Event.defaultColumns = 'title';
Event.register();

路线/查看代码

var keystone = require('keystone');

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

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

    // Set locals
    locals.section = 'event';
    locals.filters = {
        detail: req.params.detail,
    };
    locals.data = {
        details: [],
    };

  // load event data
    view.on('init', function (next) {

        var q = keystone.list('Event').model.findOne({
            slug: locals.filters.detail,

        })

        q.exec(function (err, result) {
            locals.data.detail = result;
      console.log(result);
            next(err);
        });

    });

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

2 个答案:

答案 0 :(得分:1)

事实证明我在routes/index.js中输了一个拼写错误。

编辑:我相信我忘记将其添加到routes/index.js

app.get('/event', routes.views.event);
app.get('/event/detail/:detail', routes.views.detail);

答案 1 :(得分:0)

你有一个错字。

您在此处设置 locals.data。详细信息

locals.data = {
    details: [],
};

...但是将结果保存到 locals.data。详细信息

q.exec(function (err, result) {
    locals.data.detail = result;
    console.log(result);
    next(err);
});