Bummer! You must be logged in to access this page.

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

Printer.js no longer printing to console

So for some reason all of a sudden after I move my print functions to their own file and try to run the node command on my app.js file then it doesn't want to print anything in my console. Why is that?

I am still struggling with debugging this one as nothing is printing out when I run the following command.

node app.js myusernamehere

app.js

var profile = require("./profile.js");
var 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
var https = require("https");
var http = require("http");
var printer = require("./printer.js");

function get(username) {
    //Connect to API URL (https://teamtreehouse.com/username.json)
    var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
        var body = "";
        //Read the data
        response.on('data', function(chunk) {
            body += chunk;
        });
        response.on('end', function() {
            if (response.statusCode == 200) {
                try {
                    //Parse the data
                    var profile = JSON.parse(body);
                    //Print the data
                    printer.printMessage(username, profile.badges.length, profile.points.JavaScript);
                } catch(error) {
                    //Parse Error
                    printer.printError(error);
                }
            } else {
                //Status Code Error
                printer.printError({message: "There was an error getting the profile for " + username + ".  (" + http.STATUS_CODES[response.statusCode] + ")"});
            }
        });
    });

    //Connection Error
    request.on('error', printer.printError);
}

module.exports.get = get;

printer.js

//Print out message
function printMessage(username, badgeCount, points) {
    var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in Javascript";
    console.log(message);
}

//Print out error messages
function printError(error) {
    console.error(error.message);
}

module.exports.printMessage = printMessage;
module.exports.printError = printError;

1 Answer

Can you confirm that printer.js is in the same directory as app.js and profile.js?

For debugging, try adding a console.log() command after each time you set the value of a variable, to ensure it actually matches what you expect.

Right now the only thing I can see that might be a little off is that your printError function expects an error object as its argument with the property message.

You only have one case where you're passing an object with that property, so maybe change the other two to match? Note that to do that in the last case, you'll need to pass an anonymous function as a callback with the call to the printer.printError function inside it.