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

Matt Yost
seal-mask
.a{fill-rule:evenodd;}techdegree
Matt Yost
Full Stack JavaScript Techdegree Student 13,976 Points

BODY: NaN

When I run the application in the console I do not get any of the body text.. Just BODY: NaN. Maybe I have not looked thoroughly enough, but I can't seem to find the answer anywhere. Any help would be greatly appreciated.

Thanks.

//Connect to API URL
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
  console.log(response.statusCode);
  //Read data
  response.on('data', function (chunk) {
    console.log('BODY: ', + chunk);
  });
  //Parse data
  //Print data

});

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

2 Answers

Luciano Bruzzoni
Luciano Bruzzoni
15,518 Points

Hey, try taking out the + sign on your console.log function. You can either separate your arguments with a comma or do it all together by adding the strings with the + sign. In this case, you have the + sign after the comma, so it's thinking that there's something to add, and since there is nothing before it, it returns NAN. so you can do console.log("BODY: " + chunk); or console.log("BODY: ", chunk);

Hope that solved the issue!

here's mine

var http = require('https');
var user  = 'robohat'

var request = http.get('https://teamtreehouse.com/' + user + '.json', function(response){
    console.log('status code:', response.statusCode);
    console.log('headers:', JSON.stringify(response.headers))
    response.setEncoding('utf8');
    response.on('data', function(chunk) {
      console.log('BODY: ' + chunk);
    });
});





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

JSON.stringify(response.headers) that is what got the text going I think, and setEncoding.