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

Tyler Haas
Tyler Haas
747 Points

Post variable undefined

I am getting undefined on the post object when I run the post.jade file. Here is the post.jade file

doctype html
html(lang="en")
  head
    title #{post.title}
  body
    section.post
      .container.text-right
        a(href="#").text-faded view all
        .row
          .col-lg-8.col-lg-offset-2.text-center
            h2.section-heading #{post.title}

            hr.light

            p.text-faded
              | #{post.description}
            .article
              | #{post.description}

And my app.js

'use strict';

var express = require('express'),
      posts = require('./mock/posts.json');

var app = express();

app.set('view engine', 'jade');
app.set('views', __dirname + '/views');

app.get('/', function(req, res){
  res.render('index');
});

app.get('/blog/:title?', function(req, res){
  var title = req.params.title;
  if(title === undefined) {
    res.status(503);
    res.send("This page is under construction!");
  } else {
    var post = posts[title];
    // debugger;
    res.render('post', { post: post });
  }
});

app.listen(3000, function(){
  console.log("The frontend server is running on port 3000...");
});
Casey Ydenberg
Casey Ydenberg
15,622 Points

Hmmm ... your code isn't formatting correctly. Trying editing your post and using the Preview (Eyeball) feature to see how it appears once posted.

huckleberry
huckleberry
14,636 Points

Fixed up your code there a bit. Please take a look and see how it's done for future reference :)

Cheers,

Huck - :sunglasses:

5 Answers

Brett Connolly
Brett Connolly
12,874 Points

Yeah, I didn't realize you had to put in the EXACT title (capitalization and special characters) otherwise it shows the undefined error. You wouldn't likely use such a cumbersome 'key' in normal applications, usually IDs (unless for some reason you want those hidden and you need some other way to grab or reference an object).

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

You'd think this would have been mentioned somewhere, because normally URLs are NOT case-sensitive. I've been sifting through the code for an hour now trying to figure out what is going wrong... when it just comes down to cases!

Crossfit is Cool

will work

Crossfit is cool

will NOT

Thank-you! :dizzy:

That tripped me up too. Thanks

Casey Ydenberg
Casey Ydenberg
15,622 Points

What URL are you going to when you see the error? I had to do localhost:3000/blog/I like to run! and let my browser URI encode it (ie put %20 in place of the spaces).

It would be simpler to just use ID numbers, IMO.

Rachel Black
Rachel Black
3,943 Points

Hi I am getting the same problem, had a look over the codebase on github, but not sure whats wrong, all the vars in the jade are being identified as "undefined"

Any help would be great,

Rachel

This did not help at all. My first {post: post} is still not lighting up. Please tell me how to get this render to work like the video. Thank you.

Wow. I just spent hours banging my head against this problem, going over my code dozens of times. And all along, all I had to do was change

/crossfit%20is%20cool to /Crossfit%20is%20Cool

So dumb.