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) The Request and Response Objects in Express Responses and the Response Object

Don Hamilton III
Don Hamilton III
31,828 Points

'TypeError: Cannot read property 'Swimming is great for the joints' of undefined'

Hey everyone,

I was first running into an issue where I was unable to view the any objects in the console on the Node Inspector page. I understand the idea though so I kept going, but now I'm running into a problem in which I can't retrieve individual posts. The error I get is as follows:

TypeError: Cannot read property 'Swimming is great for the joints' of undefined at /Users/donhamiltoniii/Desktop/projects/express-basics/src/app.js:16:18 at Layer.handle as handle_request at next (/Users/donhamiltoniii/Desktop/projects/express-basics/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/Users/donhamiltoniii/Desktop/projects/express-basics/node_modules/express/lib/router/route.js:112:3) at Layer.handle as handle_request at /Users/donhamiltoniii/Desktop/projects/express-basics/node_modules/express/lib/router/index.js:277:22 at param (/Users/donhamiltoniii/Desktop/projects/express-basics/node_modules/express/lib/router/index.js:349:14) at param (/Users/donhamiltoniii/Desktop/projects/express-basics/node_modules/express/lib/router/index.js:365:14) at Function.process_params (/Users/donhamiltoniii/Desktop/projects/express-basics/node_modules/express/lib/router/index.js:410:3) at next (/Users/donhamiltoniii/Desktop/projects/express-basics/node_modules/express/lib/router/index.js:271:10)

my code is below:

'use strict';

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

var app = express();

app.get('/', function(req, res) {
    res.send("<h1>I am loving Treehouse!</h1>");
});

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

app.listen(3000, function(){
    console.log("The frontend server is running on port 3000!")
});

Thanks in advance for any help

Don Hamilton III
Don Hamilton III
31,828 Points

Got it. I have:

var post = post[title];

Where instead I should have:

var post = posts[title];

Stay vigilant about proper spelling everyone! lol

1 Answer

Steven Parker
Steven Parker
229,732 Points

Congrats on finding your own issue! :+1:

Remember to update your post and add [SOLVED] to the title as an alternative to choosing a best answer.