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 trial

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

SyntaxError: Unexpected token d in JSON at position 0 undefined:1 data:data

I've checked over the code and it looks like it mirrors the

const https = require('https'); const username = "chalkers";

//Function 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); }

// 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:', data.toString();
                        });

                        response.on('end', () => {
                        // Parse the data
                        const profile = JSON.parse(body);
                          console.dir(profile);
                        // Print the data

                        });

                      });

1 Answer

Emre K.
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Emre K.
Full Stack JavaScript Techdegree Graduate 16,408 Points

Hello, Kaleshe Alleyne-Vassel

I hope you were able to solve your problem. If not, I tested your code and noticed it is slightly different than the presented one.

Presented one:

          // Read the data
    response.on("data", data => {
        body += data.toString();
    })

Yours was as follows:

       // Read the data
        response.on('data', data => {
         body += 'data:', data.toString();
        });

It works for me this way.

Thank you. I had the same issue as well, and this seemed to fix it :)

Frank Kynard
Frank Kynard
6,958 Points

Yeah me as well. not sure how I did this but Thanks