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 Getting the Response Body

Why I get 7 strings when I log out typeof body

I'm running the v10.15.3 version on Windows 10 and when I type node app.js on PowerShell I get 7 string words on above one other. Here is my code

const https = require('https');

const username = 'mbarra';

// Function to print message to console
function printMessage(username, badgeCount, points) {
    const message = `${username} has ${badgeCount} total badges(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 => {
    response.on('data', data => {
        let body = '';
        // Read the data
        response.on('data', data => {
            body += data.toString();
        });

        response.on('end', () => {
            // Parse the data
            // console.log(body);
            console.log(typeof body);
            // Print the data
        });
    });
});

2 Answers

Steven Parker
Steven Parker
229,786 Points

You have nested layers of "response.on" handlers. Remove the outer one (starting at line 13) to make the code like the video example.

Yes, it was a stupid mistake, thanks.

Steven Parker
Steven Parker
229,786 Points

Miguel Barra — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!