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 trialTyler Haas
747 PointsPost 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...");
});
huckleberry
14,636 PointsFixed up your code there a bit. Please take a look and see how it's done for future reference :)
Cheers,
Huck -
5 Answers
Brett Connolly
12,874 PointsYeah, 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
Treehouse Moderator 145,860 PointsYou'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!
Rick Klaras
20,183 PointsThat tripped me up too. Thanks
Casey Ydenberg
15,622 PointsWhat 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
3,943 PointsHi 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
Jack Kuhry
3,551 PointsThis 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.
watson13j
Courses Plus Student 9,130 PointsWow. 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.
Casey Ydenberg
15,622 PointsCasey Ydenberg
15,622 PointsHmmm ... your code isn't formatting correctly. Trying editing your post and using the Preview (Eyeball) feature to see how it appears once posted.