JavaScript Node.js Basics Building a Command Line Application Capturing Command Line Arguments

Larry Gonzales
Larry Gonzales
10,112 Points

Unexpected token N in JSON at position 0

John Lack-Wilson
John Lack-Wilson
8,156 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.

Larry Gonzales
Larry Gonzales
10,112 Points

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
170,249 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.

Larry Gonzales
Larry Gonzales
10,112 Points

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.