Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics (2015) Using Templates with Express The “response.render” Method

Aleks Dahlberg
Aleks Dahlberg
19,103 Points

For those confused with `var post = posts[title] || {};`

var post = posts[title] || {};

Is essentially the same as the below. However I have added an error-page. (if the object's title is undefined render error-page else, render post page.)

note: I am using pug not jade (same thing nearly)

//missing the rest of the blog route
} else {
    var post = posts[title];
    if ( post === undefined){
        response.render('error-page.pug');
    } else {
        response.render('posts.pug', {post: post});
    }
}
Aaron Martone
Aaron Martone
3,290 Points

Be careful. If posts.title resolves to a falsy value like: NaN (Not a Number), 0, -0, '', false, undefined, null, then your code will return an empty object always rather than that value. It's not checking if post.title is undefined, it's checking if post.title is truthy. The way Or (||) works is that it returns the first truthy value, evaluating left to right. Otherwise it returns the last falsy value. So: posts[title] || {} would return back posts.title (if it were a truthy value). But if it returned a false value, it would move to the next expression ({}), and an empty object is truthy, thus it is returned and stored into the post variable.

But when an empty object post is returned how is post.title or post.description empty and not undefined ? I believe we should receive an error 'post.title not defined' when post is empty since it has no 'title' property but that is clearly not the case. Where am I going wrong ?