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 Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Reading from Files

Anthony Trischitti
Anthony Trischitti
12,259 Points

Keep getting the error response regardless of profile name?

I realized that the error div is being returned for every user name I typed in. I am sometimes getting an error message in the console regarding events.js line 85. Not sure what is going on here?

Here is a snapshot of the workspace https://w.trhou.se/fbaaim05cd

Hi Anthony,

Can you snapshot your workspace for us? It's the arrow icon next to the eye icon in the workspace window. That way we can see what's going on.

Elian

Peter Smith
Peter Smith
12,347 Points

theres no events.s file in your workspace

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

profile.js

var EventEmitter = require("events").EventEmitter;
var http = require("https"); 
var util = require("util");
// ...code ommited...
// line 23
            profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));

STATUS_CODES is one of the few things node's https doesn't replicate from http, and using https in it's place here is causing the error. (Check the stack trace, it should mention profile.js at or near the top)

Wilson Usman
Wilson Usman
35,206 Points

So...

If I change var http = require() from http to https, I still keep getting the same error.

What is the solution here?

Juan Francisco Andrade Álvarez
Juan Francisco Andrade Álvarez
23,997 Points

Having the same issue, i solved it using both https for the API call and http for getting the status code:

............
var https = require("https");
var http = require("http");
..........
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {

        var body = "";

        if (response.statusCode !== 200) {

            request.abort();
            //Status Code Error
            profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
        }
...........

I don't know if this is a good practice but solved the issue.....