【问题标题】:Initialises list object in locals in middleware but its not working在中间件的本地初始化列表对象,但它不起作用
【发布时间】:2018-10-13 21:01:29
【问题描述】:

我正在尝试在中间件中的局部变量中初始化列表对象以在索引视图模板中显示局部变量,但它不起作用。

exports.initLocals = function (req, res, next) {

res.locals.navLinks = [
                { label: 'Home', key: 'home', href: '/' },
                { label: 'Contact', key: 'contact', href: '/contact' },
                { label: 'Products', key: 'products', href: '/products  ' },
];
res.locals.cart = keystone.session.cart;            
res.locals.user = req.user;

keystone.list('Category').model.find().sort('name').exec(function (err, results) {

    if (err || !results.length) {
        return next(err);
    }
    locals.data.categories = results;
});

next();

};

【问题讨论】:

    标签: node.js express keystonejs


    【解决方案1】:

    最后,我找到了解决方案。我犯了两个错误。首先,我在列表查询范围之外调用了next() 方法。

    其次,我没有正确引用 locals.categories

    //get top three category
    keystone.list('Category').model.find().sort('name').limit(3).exec(function (err, results) {
        if (err || !results.length) {
            return next(err);
        } else {
                res.locals.categories = results;
                    next();
                }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 2020-05-16
      • 2016-01-26
      • 2010-12-20
      • 2016-11-09
      • 2018-08-12
      相关资源
      最近更新 更多