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 trialSandi Wu
6,752 PointsUnexpected token u in JSON
I got this error message. Can anyone help me solve this problem? Thanks!
***Here are the error messages.
undefined:1
function (encoding, start, end) {
^
SyntaxError: Unexpected token u in JSON at position 1
at JSON.parse (<anonymous>)
at IncomingMessage.response.on (/home/treehouse/workspace/app.js:23:30)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
***Here are my codes.
// 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');
//username
const username = 'sandi'
// Function to print message to console
function printMessage(username, badgeCount, points) {
const message = `${username} has ${badgeCount} badge(s) and ${points} points in JavaScript.`;
console.log(message);
};
// 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);
console.dir(profile);
});
// Print the data
});
1 Answer
KRIS NIKOLAISEN
54,972 PointsYou have two corrections to make:
1) toString
needs to be followed by parentheses
body += data.toString();
2) If you check the link to your profile the username is "sandiwu". Just "sandi" returns page not found.
Therefore:
const username = "sandiwu"
Sandi Wu
6,752 PointsSandi Wu
6,752 PointsThanks for the help!