2017-08-08 99 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(); 
     }); 
    }; 
相關問題