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 Node.js Basics (2014) Building a Command Line Application Handling Errors in Node

stage 2 cmd line

var http = require("http");

http.get("http://teamtreehouse.com/chalkers.json", function(response){ console.log(response.statusCode); });

var request = http.get; request.on = ('error', function(res) 0; console.error(error);

code does't work.

app.js
var http = require("http");

http.get("http://teamtreehouse.com/chalkers.json", function(response){
  console.log(response.statusCode);
});

var request = http.get;
request.on = ('error', function(res) ) ;
console.error(error);

4 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hello, ellie adam

Part 1 of this challenge

Create a variable request that stores the result of the get method.

What you should do is assign the result of the entire http.get method to a request variable.

var http = require("http");

var request = http.get("http://teamtreehouse.com/chalkers.json", function(response){
  console.log(response.statusCode);
});

And part 2 of the challenge

On a new line use the on method to listen for the error event. Pass in a callback function with one parameter of error.

Now, for this part, call the on method on the request variable, the callback function should take an error argument, function body can be empty.

var http = require("http");

var request = http.get("http://teamtreehouse.com/chalkers.json", function(response){
  console.log(response.statusCode);
});

request.on('error', function(error){});

Hope this helps, review Andrew's lecture if you are still having trouble with it, the lecture covers this topic in great length.

Cheers.

Thanks Li! Sometime I am not sure where these ( ) belong. You wrote mnimum code and solved it. One last question on third stage it is asking for console.error when I type console.error(errorMassage); it give me message error is not defined!

William Li
William Li
Courses Plus Student 26,868 Points

Hi, ellie adam , alrite, part 3 of the challenge

Finally, in the error callback, use the error method on the console to print out the error message.

Remember what I wrote in part 2, we left the callback function body empty, in part 3, we need to add sth to its body.

request.on('error', function(error){
  console.error(error.message); //use the error method on the console to print out the error message.
});

The correct answer for this frustrating quizzie..............

request.on('error', function(error){ console.error(error.message); //use the error method on the console to print out the error message. });

amadeo brands
amadeo brands
15,375 Points

This is a bit of a strange question ... I think it needs fixing.

There are 2 problems you will need to include the https not the http ... and also the qwestion asks here not correct and confucing.

//Problem: We need a simple way to look at a users badge count and Javascript points
//Solution: Use Node.js to connect to Treehouse API to get profile information to print out 
var https = require('https');
var username = "amadeobrands";

function printmessage(username, badgeCount, points) {
  var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript";
    console.log(message);
}

//Connect to API Threehouse (https://teamtreehouse.com/[username].json
var reqwest = https.get('https://teamtreehouse.com/' + username + '.json', function(response) {
  console.log("Amadeo is great:", response.statusCode);

  /*
  response.on('data', function(d) {
    process.stdout.write(d);
  });
  */

//Read data from the response
//Parse the data
//Print the data
});

reqwest.on("error", function(error) {
  console.error(error.message);
});