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 trialLarry Gonzales
10,112 PointsUnexpected token N in JSON at position 0
Larry Gonzales
10,112 PointsAhh! 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
231,261 PointsIf 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. Or watch this video on code formatting.
Larry Gonzales
10,112 PointsThank 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.
John Lack-Wilson
8,181 PointsJohn Lack-Wilson
8,181 PointsHi Larry, it might be better to copy paste your code in here as Cloud 9 is requiring a login to be able to view.