JavaScript Node.js Basics Building a Command Line Application Capturing Command Line Arguments

Kristian Woods
Kristian Woods
22,208 Points

Am I not connecting to the API correctly?

const https = require('https');

    function printMessage(username, badgeCount, points) {
        const message = `username: ${username} - badgeCount: ${badgeCount} - points: ${points}`;
        console.log(message);
    }


function getProfile(username) {
    const request = https.get(`https://teamtreehouse.com/${username}.json`, (response) => {
        let body = "";


        response.on('data', (dataChunck) => {
            body += dataChunck.toString();
        });

        response.on('end', () => {
            const profile = JSON.parse(body);
            printMessage(username, profile.badges.length, profile.points.JavaScript);
        })
    });

    request.on('error', error => console.error(`problem with request: ${error.message}`));
}

const users = process.argv.slice(2);

users.forEach(printMessage);

1 Answer

Neil McPartlin
Neil McPartlin
14,634 Points

Hi Kristian. The getProfile function is not getting called because the last line of your code has 'printMessage' as the argument rather than 'getProfile'. So change this to...

users.forEach(getProfile);

...and all should be well.