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 trialMaria Campbell
8,641 PointsI got the wrong error when testing out printError in stage_2_video_6 with message object.
This is the code I have in app.js (which includes the code we had to modify because treehouse is now https: this video should really be re-made!):
//Problem: We need a simple way to look at a user's badge count and JavaScript points //Solution: Use Node.js to connect to Treehouse's API to get profile information to print out var https = require("https"); var username = "mariacampbell23";
// print out message function printMessage(username, badgeCount, points) { var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript"; console.log(message); }
// print out error messages function printError(error) { console.error(error.message); }
//Connect to the API URL (https://teamtreehouse.com/username.json) var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) { // inspect the response object var http = require("http");
http.STATUS_CODES; var body = ""; //Read the data response.on('data', function(chunk) { body += chunk; }); // 'end': event submitted by the system once the end has happened. With this chunk of // code the whole body prints out to terminal instead of simply in chunks. response.on('end', function() { if(response.statusCode === 200) { // if we want to pause the code execution, we use the keyword try and then wrap the code // that I want to try in a block. // If an error occurs, it throws an error object that I can catch. try { var profile = JSON.parse(body); printMessage(username, profile.badges.length, profile.points.JavaScript); /console.dir(profile);/ // this does a pretty print to figure out the total number of badges and points. Brings back all profile info as JSON object } catch(error) { // parse error printError(error); } } else { // status code error printError({message: "There was an error getting the profile for " + username + ". (" http.STATUS_CODES[response.statusCode] + ")"}); } }); //Parse the data //Print the data }); // Connection error handler request.on("error", printError);
The specific code in question is:
} else { // status code error printError({message: "There was an error getting the profile for " + username + ". (" http.STATUS_CODES[response.statusCode] + ")"}); }
This is what I got back in TERMINAL (NOT Workspaces, don't use that because it is not the real world):
node app.js ⏎ /Users/mariacam/Development/bitbucket-repos/nodejs-basics/stage_2_video_6/app.js:45 printError({message: "There was an error getting the profile for " + username + ". (" http.STATUS_CODES[response.statusCode] + ")"}); ^^^^
SyntaxError: Unexpected identifier at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:414:25) at Object.Module._extensions..js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Function.Module.runMain (module.js:467:10) at startup (node.js:136:18) at node.js:963:3
So I would like to know what is wrong here. Anyone else had this issue? Thanks!
1 Answer
Maria Campbell
8,641 PointsI found MY error. Instead of:
printError({message: "There was an error getting the profile for " + username + ". (" http.STATUS_CODES[response.statusCode] + ")"});
It should be:
printError({message: "There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"});
I inadvertently omitted a + in the concatenation.