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 2017 Handling Errors in Node Handling the Error Event in Node

Rishi B
Rishi B
13,866 Points

what is this error? SyntaxError: Unexpected end of JSON input

var https = require('https');

function printMessage(username, badges, points){
    const message = `${username} has ${badges} badges and ${points} points`;
    console.log(message);
}

function getProfile(username){
        var request = https.get(`https://wwwteamtreehouse.com/${username}.json`, response => {

                                var body = '';
                                response.on('data', data => {
                                    body += data.toString();        
                                })

                                response.on('end', () => {
                                    const profile = JSON.parse(body);
                                    printMessage(username, profile.badges.length, profile.points.JavaScript);
                                })      
                        });
            request.on('error', error => console.error(error));

}


const users = process.argv.slice(2);
users.forEach(getProfile);
Henry Blandon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Henry Blandon
Full Stack JavaScript Techdegree Graduate 21,521 Points

I 'm here late, but for those coming to look for answers. I ran the code above Rishi B posted and it actually runs fine. i replace the wwwteamtreehouse.com for 333teamtreehouse.com and the the error message shows properly. i set it back to teamtreehouse.com and ran the code with my username and the code runs fine. just like Carl Orre posted.

3 Answers

I had the same problem. In order to get the same error message as in the video, just change www to basically anything else. I think that the reason for the error is that in the time since the release of the video they have bought the domain wwwteamtreehouse.com which redirects to the home page. The home page does not have JSON data, resulting in a JSON related error and not the desired Unhandled 'error' event.

Thanks, I would have never guessed that in a million years but it fixed it for me :)

Rishi B
Rishi B
13,866 Points

Nope. I just moved on. I did get the different error. I didn't get the error that the tutorial mentioned.

i kept trying random urls that don't exist and still got this json error. just ended up with an undefined object every time. not sure why it wasn't catching the bad urls