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

Unexpected token N in JSON at position 0

John Lack-Wilson
John Lack-Wilson
8,158 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 ( const request = https.get(${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);

}); }


1 Answer

Steven Parker
Steven Parker
173,991 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.