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

Unexpected Syntax Error ";" Could you guys help me figure out where this error is in my code?

I'm getting this error on the console:

treehouse:~/workspace$ node app.js
/home/treehouse/workspace/app.js:32
});
^
SyntaxError: Unexpected end of input
at Object.exports.runInThisContext (vm.js:5
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)

Here is my code:

/* Problem: We need a simple way to look at a user's badge count & JavaScript Points / / Use Node.js to connect to Treehouse's API to get profile information to print out. */ var https = require("https"); var username = "fredericcamara"; 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 USR (https://teamtreehouse.com/username.JSON) */

var request = https.get("http://teamtreehouse.com/" + username + ".json", function (response) { var body = ""; // Read the data response.on('data', function (chunk) { console.log('BODY: ', chunk); body += chunk; }); response.on('end', function () { var profile = JSON.parse(body); printMessage(username, profile.badges.length, profile.points.JavaScript); });
});
/* Parse the data / / Print the data */ request.on("error", function(error) { console.error(error.message); });

Steven Parker
Steven Parker
230,233 Points

Please format your code.

Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the text entry area. :arrow_heading_down:

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

I reformatted your code. This works:

var https = require('https');
var username = 'thomasnilsen';

var request = https.get("https://teamtreehouse.com/" + username + ".json", 

function(response) { 
    var body = ""; 

    // Read the data 
    response.on('data', function (chunk) { 
        //console.log('BODY: ', chunk); 
        body += chunk; 
    }); 

    response.on('end', function () { 
        var profile = JSON.parse(body); 
        printMessage(username, profile.badges.length, profile.points.JavaScript); 
    });
});

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


function printMessage(username, badgeCount, points) { 
    var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript"; 
    console.log(message); 
};
Steven Parker
Steven Parker
230,233 Points

:point_right: Check for a missing or extra brace "{}" or parenthesis "()".

I haven't tried to read the unformatted code, but a missing or extra brace or parenthesis is a very likely cause of a "Unexpected end of input" error.