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

Christopher Simmons
seal-mask
.a{fill-rule:evenodd;}techdegree
Christopher Simmons
Full Stack JavaScript Techdegree Student 13,336 Points

Node get api issue with treehouse api.. it works in vs code debugger but not from command line.

my code works in the vs debugger when i step through it

but not from the terminal...

calling it with "node app.js chrissimmons06"

I see on the console...

[ 'chrissimmons06' ]
step1 (https://teamtreehouse.com/chrissimmons06.json)
step2 (404)
There was an error chrissimmons06 (404) ()

const https = require('https');
const http = require('http');
const username = "christophersimmons6"

function printMessage(username, badgeCount, points) {
    const msg = `${username} has ${badgeCount} total badge(s) and ${points} points in Javascript`;
    console.log(msg)
}

function get(username) {

    try {
        const message = `step1 (${`https://teamtreehouse.com/${username}.json`})`;
        printError(message);
        const request = https.get(`https://teamtreehouse.com/${username}.json`, (res) => {

            printError( `step2 (${res.statusCode})`);

          if (res.statusCode == 200){
            let body = "";
            res.on('data', (data) => {
                body += data.toString();
            });

            res.on('end', () => {
                const profile = JSON.parse(body);
                printMessage(username, profile.badges.length, profile.points.JavaScript)
                //console.log(body);
            });
        } else{
          message = `There was an error ${username} (${res.statusCode}) (${res.url})`;
          printError(message);

        }

        }).on('error', (e) => {
            printError(`Problem with request: ${e.message}`);
        });
    } catch (e) {
        printError(`Problem with initial request: ${e.message}`);
    }

};

module.exports.getProfile  = get;

calling code

const profile = require('./profile.js') 
const users = process.argv.slice(2)
console.log(users)
users.forEach(profile.getProfile);

1 Answer

Lee Vaughn
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Lee Vaughn
Treehouse Teacher

Hi Christopher! I replied to your post in the Techdegree Slack with more info but the short version of the answer for anyone who might see this is that there seems to be an issue with the username you are using in your node app.js command. It is returning "Not found" but if you use the same command with a different username (like chalkers) it works like normal. Just double-check the username you are using here and you should be golden. :thumbsup: