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

nathan kolody
nathan kolody
12,134 Points

There was an error getting the profile chalks (ok)... node question

I keep getting an error trying to access the api for user chalkers. From the if the statement the error say its ok, but I can't figure out to fix it, here's the code:

app.js: const profile = require('./profile.js');

const users = process.argv.slice(2); users.forEach(profile.get);

profile.js

// 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'); //Require http module. const http = require('http'); //Print error message function printError(error){ console.error(error.message); }

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

function get(username) { try { // Connect to the API URL (https://teamtreehouse.com/username.json) const request = https.get(https://teamtreehouse.com/${username}.json, response => { if(response.status === 200){
let body = ""; // Read the data response.on('data', data => { body += data.toString(); });

                        response.on('end', () => {
                                    try{
                          // Parse the data
                          const profile = JSON.parse(body);                            
                          // Print the data
                          printMessage(username, profile.badges.length, profile.points.JavaScript);
                              }   catch(error)  {
                                printError(error);
                                }                  
                            });

} else { const message = There was an error getting the profile ${username} (${http.STATUS_CODES[response.statusCode]}); const statusCodeError = new Error(message); printError(statusCodeError); } }); request.on('error', printError); } catch (error) { printError(error); } }

module.exports.get = get;

Any help would be greatly appreciated since I'm sort of grasping the concept but not really at the same time.

You'll probably have to do a better job at markdown formatting to get a response. A link to the video/challenge would help as well.

1 Answer

nathan kolody
nathan kolody
12,134 Points

Hey Kris, thanks for the reply. I found the error which was in the if statement. I put response.status and not response.statusCode. I apologize for the code format, Iā€™m not sure what I did there. Have a good week!