Why doesn't this data display?
Problem
I have the following JSON object:
{
user:
{
city: 'San Francisco',
country: 'US',
displayName: 'Bernard',
weightUnit: 'METRIC'
}
}
It comes back in the following piece of code, i.e. as a string:
var response = results[0];
I send it to the view like this:
res.render('user', {title: 'User Details', result: JSON.parse(response)});
In the view, no matter what I do, I cannot access Displayname.
All I want is to this in my jade template:
h1 Hi, #{displayName}
And I keep getting user Undefined, undefined of undefined, etc.
No matter how I try and access that displayName, jade/express simply cannot get to it.
Anyone have any ideas how I'd do this please?
Solution
That object that you pass in your render
call becomes the context your Jade file uses. Thus, at your root, you have title
and result
. See where this is going?
Try h1 Hi, #{result.user.displayName}
.
Discussion
View additional discussion.