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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsTrying to make an error event handler on node.js
Hi all,
So I'm a little stumped as to why this shouldn't be working.
I'm pretty sure I'm using the right methods variable request
and method in on
and the correct syntax for a callback function. What am I missing? :)
Thanks.
var http = require("http");
http.get("http://teamtreehouse.com/chalkers.json", function(response){
console.log(response.statusCode);
});
var request = http.get;
request.on("error", function(error) {
});
2 Answers
Henrique Zafniksi
8,560 PointsEvaluate the request variable to the method get, on the http variable. Making a later reference will create another instance instead of assigning to it.
var request = http.get("http://teamtreehouse.com/chalkers.json", function(response){
console.log(response.statusCode);
});
In case of errors, try this, as it will console the specific error to you:
request.on("error", function(error){
console.error(error.message);
});
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi Henquitw,.
Well I'm still stumped, and this the first time i've had in 5 days to come back to it.
This ought to be a simple task....
Aha, I've since worked it out.
We were supposed to add var request =
to the third line of my code. As you said. It took a while for me to work it all out but the ligthbulb in my brain seems to be switched on now.