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

Getting an empty object when trying to get profile data from Treehouse JSON

I'm working on the "Build a Simple Dynamic Website" course and am stuck at the section on routing. I have both my home and user routes built and established in a separate file from my app,js.

My set up runs fine, meaning that the functions within router.js are successfully called from app.js when the URL (https://teamtreehouse.com/[username] is passed... however, an empty object is returned from the Treehouse DB.

Here's my workspace - https://w.trhou.se/5n4r7ws58b

Any thoughts on this?

Hey Zachary,

Are you getting any error messages/console logs when you run your app? I received the message "Failed to load resource: the server responded with a status of 405 (Method Not Allowed)"

I got the same response in both Chrome and Safari.

3 Answers

Steven Parker
Steven Parker
243,656 Points

How are you running this?

I tried it with the command "node example_profile.js" and it gave me back the profile in JSON format.

I've been using node Javascript/app.js. I changed up the directory structure when I started the project. Maybe this was a bad idea?

Hey Zachary,

I tried running your app using node Javascript/app.js. And when I go to localhost:3000 I receive this message text:

Header
Search 
Footer

Which actually makes sense if you look at your router.js file. You're telling it to write those things when you go to the "/" route of your page. Maybe you're wanting to direct users to your index.html file when this page is visited?

This is the expected behavior when navigating to the home route ("/").. The issue is, when I try to navigate to a user's profile I get an error.

As of right now, when I type in /[anyUsername], it should redirect the client through the user function which should log a profile object to the console. Problem is, the profile object is empty and I can't for the life of me figure out why. I think it's something obvious I'm missing but I can't seem to find it.

Steven Parker
Steven Parker
243,656 Points

You'll laugh when you see the issue here.

In router.js, in the callback from the "end" event, you parse a few fields from the the JSON into a values object, but then nothing is done with that object. The only thing written to the response is the string "Finishing...".

I added a little code to see the contents of the value object and they contained student data as expected.

I'm imagining the sound of a palm smacking a forehead. :laughing:

The issue here isn't that I'm not using the data that I parse, but that there is no data returned as a result of the parsing. When I do console.dir(STUDENT_PROFILE) there is an empty Profile object which should at that point be filled with student data.

I have no idea why the object is empty.

Steven Parker
Steven Parker
243,656 Points

I'm not aware than an EventEmitter exposes anything interesting to see, it's just good for attaching listeners. I certainly would not expect it to contain any response data.

Your response data arrives through your parameter profileJSON, which is then used to create your values object. And the contents of that seem to be as expected.