Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Node.js Basics (2014) Building a Command Line Application Getting the Response Body

Hi all, please help, I'm really confused by a reference error: response is not defined -

I've changed my var to https, but it looks like something has changed in the docs from when the video was made - I've tried using all of the old 'response.on' code as well as the new and even mixed and matched a bit - but i seem to be stuck - can anyone help please?

// Look at a user's badge count and JS points
// Use node.js o connect Treehouse's API and print profile information.

var https = require("https");
var username = "graemehunt";


function printMessage(username, badgeCount, points) {
    var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in Javascript";
      console.log(message);
}
// Connect to the API URL (http://teamtreehouse.com/username.json)
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
    console.log(response.statusCode);
});
// Read the data
response.on('data', function(chunk) {
  console.log('BODY:  ${chunk}');
});
// Parse the data
// Print the data

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

Hey graemehunt, could you please edit your post and format the code so that it's more readable? Right now it's just a blob and hard to read :)

Hi thank you christianandersson, my first post with code - I should have checked the cheatsheet first!

Ah, that's better! :)

1 Answer

You're getting an error telling you that the variable response is undefined - basically that it's an unknown variable that hasn't been declared before.

This is because response was used as the argument in your callback function and is scoped to that function (i.e it's only "visible" inside that function, but not outside).

I believe your code should work if you move some of your code inside your callback, rather than outside:

// Look at a user's badge count and JS points
// Use node.js o connect Treehouse's API and print profile information.

var https = require("https");
var username = "graemehunt";


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

// Connect to the API URL (http://teamtreehouse.com/username.json)
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
   console.log(response.statusCode);
   // Read the data
   response.on('data', function(chunk) {
      console.log('BODY:  ${chunk}');
   });
   // Parse the data
   // Print the data
}); //<---------- callback ends here
request.on("error", function(error){
   console.error(error.message);
});

Almost. The request.on() part should be outside of the https.get(), after the request. This way, you're not calling request#on() every time you get a response.

Oh yeah, I thought it was another response.on thingy - words too similar :D. Thanks.

excellent! thank you very much for your help - that seems to have done the trick : - )