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?
Problem courtesy of: Bernard
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}
.
Solution courtesy of: juanpaco
注意:本文来自Node.js Recipes。本站无法对本文内容的真实性、完整性、及时性、原创性提供任何保证,请您自行验证核实并承担相关的风险与后果!
CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题,请通过[eMail]联系我们处理,如需商业授权请联系原作者/原网站。
CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题,请通过[eMail]联系我们处理,如需商业授权请联系原作者/原网站。