Jade template does not seem to be getting data from Express

一个人想着一个人 提交于 2019-12-11 23:55:25

问题


In the get handler, I create an object, plans, from my data source. Then I do:

console.log(plans);
res.render('dashboard', plans);

It prints:

{ '0': 'mobile_basic',
  '1': 'mobile_basic',
  '2': 'landline_basic',
  '3': 'landline_mid',
  '4': 'internet_mid' }

This means that this object is being passed into the template. In the dashboard Jade template, I have:

if plans
  ul
    each val, index in plans
      li= index + ': ' + val

This never shows up. plans is undefined in the template's context. What am I missing?

P.S. I have tried using res.render('dashboard', {data: plans}); and replacing plans in the template with data.plans. Still doesn't work.


回答1:


The name of the variable holding your data doesn't actually get passed along through res.render() to the view engine. Only its value does.

What the view can use are the keys/properties specified within the locals:

res.render('dashboard', { plans: plans });
//                        ^^^^^

These keys will become the local variables within the view.


When you attempted to use:

res.render('dashboard', { data: plans });

The view could have in turn used data to access the collection from plans:

if data # <--
  ul
    each val, index in data # <--
      li= index + ': ' + val


来源:https://stackoverflow.com/questions/28423653/jade-template-does-not-seem-to-be-getting-data-from-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!