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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Ben McMahan](https://secure.gravatar.com/avatar/a3f452836f34357aed2300ea37b640ee?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Ben McMahan
7,921 Pointsprofile.js crashing when checking status code
There's one line that seems to be crashing everything in profile.js
-
profileInstanceContext.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
If I comment this line out, everything works. Here is the complete profile.js
file, it was originally copied from the forum as Andrew's version was so deprecated.
const EventEmitter = require("events");
const https = require("https");
const http = require("http");
const util = require("util");
class Profile extends EventEmitter {
constructor(username) {
super();
// need for calling the methods of EventEmitter
let profileInstanceContext = this;
const request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
let body = "";
if (response.statusCode !== 200) {
request.abort();
//Status Code Error
profileInstanceContext.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
}
//Read the data
response.on('data', function (chunk) {
body += chunk;
profileInstanceContext.emit("data", chunk);
});
response.on('end', function () {
if(response.statusCode === 200) {
try {
//Parse the data
const profile = JSON.parse(body);
profileInstanceContext.emit("end", profile);
} catch (error) {
profileInstanceContext.emit("error", error);
}
}
}).on("error", function(error){
profileInstanceContext.emit("error", error);
});
});
}
//Connect to the API URL (https://teamtreehouse.com/username.json)
}
module.exports = Profile;
Stack trace:
Emitted 'error' event on Profile instance at:
at ClientRequest.<anonymous> (C:\Users\Benjamin\WebstormProjects\dynamic\profile.js:24:28)
at Object.onceWrapper (node:events:510:26)
[... lines matching original stack trace ...]
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)