2017-08-08 42 views
0

我使用KeystoneJS与视图的把手。我一直在试图从MongoDB获得一份公司名单,以加载导航栏,但没有成功。我知道去哪里更新导航链接,我可以在单个页面上从mongodb加载公司。我想在导航的所有页面上加载公司列表,并且compangies列表不是静态的。在导航渲染之前,我会在哪里放置代码以加载数据?如何在使用KeystoneJS加载页面之前从mongo加载数据?

回答

0

您可以在路由/ middleware.js的 'initLocals' 中间件,这将路由控制器之前运行添加自己的局部变量执行:

路线/ middleware.js

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

    var locals = res.locals; 

    locals.user = req.user; 

    // Add your own local variables here 

    next(); 

}; 

结帐的常见的途径中间件节中的Keystone.js文档: http://keystonejs.com/docs/getting-started/

0

我国际泳联lly得到它的工作

exports.initLocals = function (req, res, next) { 
     res.locals.navLinks = [ 
      { label: 'Accueil',   key: 'home',  href: '/' }, 
      { label: 'Projets',   key: 'projects', href: '/projects' }, 
      { label: 'Contacts',  key: 'users',  href: '/contacts' }, 
      { label: 'Livrables',  key: 'deliverables',href: '/deliverables' }, 
      { label: 'Fichier sources', key: 'sources',  href: '/sources' } 
     ]; 

     //Get available companies for the user 
     var view = new keystone.View(req,res); 
     view.query('companies', keystone.list('Company').model.find().populate('project').sort('title')).then(function (err, results, next) { 
      if (err) return next(err); 
      next(); 
     }); 
     view.render(function(){ 
      next(); 
     }); 
    }; 
相关问题