问题
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