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 Parsing JSON

Maciej Sitko
Maciej Sitko
16,164 Points

JSON.parse won't work as expected

I am on Node.js Basics with Andrew Chalkey (parsin JSON step) and JSON.parse(body) would not work as expected with treehouse json format because it returns:

undefined:1 undefined{"name":"Andrew Chalkley","profile_name":"chalkers","profile_url":"ht ^ SyntaxError: Unexpected token u at Object.parse (native) at IncomingMessage.<anonymous> (/Users/maciejsitko/Documents/nodecourse/basics/app.js:27:22) at IncomingMessage.emit (events.js:129:20) at _stream_readable.js:908:16 at process._tickCallback (node.js:355:11)

On the contrary, logging out console with pure body works completely fine.

Please give us your share your JS file.

Maciej Sitko
Maciej Sitko
16,164 Points
var http = require('http');

var username = "maciejsitko";



function printMsg(username, badgeCount, points) {

  var msg = username + " has " + badgeCount + ' badges and ' + points + ' points in Javascript';
  console.log(msg);

}

function printError(error) {
  console.error(error.message);



}


var request = http.get('http://teamtreehouse.com/' + username + '.json', function(response) {

  console.dir(response.statusCode);
  var body;
  var profile;
  response.on('data',function(chunk) {
    body += chunk;

  });
  response.on('end',function() {
    if(response.statusCode === 200) 
    try {
      profile = JSON.parse(body);
      console.log(profile);

    } catch(error) {

      printError(error);

     }

  });
});


request.on('error', printError);

2 Answers

Maciej Sitko
Maciej Sitko
16,164 Points

Okay I know what's the issue; the issue is variables profile and body not declared as empty strings of type String (i.e. var foo = "").

Sonar Ko
Sonar Ko
11,162 Points

But why and what make the issue ?

Mariusz Dabrowski
Mariusz Dabrowski
6,132 Points

Can anybody elaborate on this? I ran into the same issue and magically changing:

  var body;

to:

  var body = '';

Fixes things.

alex mattingley
alex mattingley
7,508 Points

Hi Mariusz,

I think I may be able to provide some insight. It has to do with how javascript treats variable types. If you declare body, but you don't set it equal to an empty string, it defaults to the value of undefined.

In the problem, we are adding chunk to body. If you add undefined to a string like "Hello world" you will actually get "undefinedHello World". What you are trying to get is obviously just "Hello World". However, if you set body to an empty string, then you will get what you are looking for:

"hello world"

Does that help?