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
Colin Stodd
6,257 PointsKeep getting a TypeError: Cannot read property 'title here' of undefined.
Very frustrating and I dont know why...
'use strict';
var express = require('express'),
posts = require('./mock/posts.json');
var app = express();
//Creates a server request.
app.get('/', function (req, res) {
res.send("<h1>Im creating my first express app! Looking good</h1>");
});
app.get('/blog', function (req, res) {
res.send(posts);
});
app.get('/blog/:title', function (req, res) {
var title = req.params.title;
var post = req.posts[title];
res.send(post);
});
//tells server request to listen on '3000'.
app.listen(3000, function () {
console.log('*** The frontend server is running on port (3000) ***');
});
{
"I like to run": {
"title": "I like to run",
"description": "Running is great for you, It's hard to get out there and do it but I'm always happy when I do."
},
"Fake content": {
"title": "This is a bunch of fake content",
"description": "Put a bird on it hammock schlitz readymade art party. Single-origin coffee viral raw denim retro microdosing everyday carry small batch kickstarter humblebrag cardigan kogi tacos stumptown. Ennui dreamcatcher kogi yuccie blue bottle cardigan, intelligentsia pitchfork migas trust fund mumblecore umami cornhole. Pug brunch skateboard crucifix post-ironic williamsburg, lo-fi PBR&B. VHS everyday carry gentrify, flexitarian pour-over skateboard selfies. Narwhal skateboard twee 3 wolf moon, tacos seitan cred. Roof party pork belly hella fingerstache ramps, normcore blue bottle blog lo-fi street art tacos ethical aesthetic."
}
}
1 Answer
LaVaughn Haynes
12,397 Pointsremove req
app.get('/blog/:title', function (req, res) {
var title = req.params.title;
//remove req. below
//var post = req.posts[title];
var post = posts[title];
res.send(post);
});
Colin Stodd
6,257 PointsColin Stodd
6,257 PointsThat worked! Thank you very much. I put this down temporarily but will now come back to it.