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

Nick Prodromou
Nick Prodromou
12,824 Points

Extra Credit - Break out print functions into separate modules

Hi Community,

I had a go at creating some extra modules to make the code more modular, here is my code:

My files are:

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 JS points
//Use node.js to connect to Treehouse's API to get profile information to print out

var https = require('https');
var print = require('./print.js')
var printError = require('./print_error.js')
//Print out message


function get(username) {
  //Connect to the API url
  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 {
          //attempt to Parse the data as JSON
          var profile = JSON.parse(body);
          printMessage(username,profile.badges.length, profile.points.JavaScript)
        } catch (error) {
          //Catch any Error in Parsing
          printError(error);
        }
      } else {
        //Catch any error in status code
        printError({message: `there was an error getting the profile for ${username} (${response.statusMessage})`})
    }
    })
    //Parse the data
  })
  //error handling
  request.on('error', printError)
}

module.exports.get = get;

print.js

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

module.exports.print = printMessage

print_error.js

function printError(error){
  console.error(error.message);
}

module.exports.printError = printError

I ran

$ node app.js nickprodromou

and I get this error:

TypeError: listener must be a function
    at ClientRequest.addListener (events.js:210:11)
    at get (/Users/nickpee/Desktop/sites/Treehouse/nodejs-basics/profile.js:36:11)
    at Array.forEach (native)
    at Object.<anonymous> (/Users/nickpee/Desktop/sites/Treehouse/nodejs-basics/app.js:4:7)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:141:18)

I'm guessing i've gotten my module pattern all wrong, but could someone please shed some light as to why this might not be passing?

Thanks!

_also, I did this locally.. and everything else worked up until this point~