Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Why doesn't this data display?

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?

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

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Why doesn't this data display?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×