Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Node.js Basics 2017 Building a Command Line Application Parsing JSON

Sandi Wu
Sandi Wu
6,752 Points

Unexpected 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

You 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
Sandi Wu
6,752 Points

Thanks for the help!