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 Building a Command Line Application Capturing Command Line Arguments

Unexpected token N in JSON at position 0

John Lack-Wilson
John Lack-Wilson
8,181 Points

Hi Larry, it might be better to copy paste your code in here as Cloud 9 is requiring a login to be able to view.

Ahh! ok, I've pasted code...

//Problem: We need a simple way to look at a user's badge count and Javascript points //Solution: Use Node.js to connect to treehouse's API to get profile information to print out

//Require https module const https = require('https');

//funtion to print message to console function printMessage(username, badgeCount, points) { const message = ${username} has ${badgeCount} total badge(s) and ${points} points in JavaScript; console.log(message); }

function getProfile(username){
// Connect to the API URL (https://teamtreehouse.com/username.json) const request = https.get(https://teamtreehouse.com/${username}.json, response => { let body = ""; // Read the data response.on('data', data => { body += data.toString(); });

response.on('end', () =>{
  // Parse the data
  const profile = JSON.parse(body);
  // Print the data
  printMessage(username, profile.badges.length, profile.points.JavaScript);
});

}); }

getProfile("larrygonzales89");

1 Answer

Steven Parker
Steven Parker
231,059 Points

If you want to handle error responses gracefully, you'll need to test for them before trying to parse them as JSON.

In the meantime, instead of using the non-existent user name "larrygonzales89", try looking up your actual user name "larrygonzales84".

And for future questions, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Thank you for the assistance. It worked just by changing the number 89 to 84 in the username, which I didn't even catch the typo till now, thanks again Steven.