JavaScript Node.js Basics Handling Errors in Node Handling Errors in Node

Javier MARQUEZ
Javier MARQUEZ
11,877 Points

I understand really well, but I am stuck in this challenge. Cannot read property 'hasCallExpression' of undefined

I tried to enter the exact same fat arrrow syntax, it should be working but it doesn't, I hate when this happens. I am out of ideas. Could anyone give me a hand.

app.js
const https = require("https");

const request = https.get("https://teamtreehouse.com/chalkers.json", response => {
  console.log(response.statusCode);
});
request.on("error", (error) => console.error($error.message));

7 Answers

Javier MARQUEZ
Javier MARQUEZ
11,877 Points

Thanks a lot for your help, you are the best.

I finally got it, like this but actually outside the const request

request.on("error", (error) => {
    console.error(error.message)
  });
Raphaël Seguin
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,227 Points

Hi, maybe you could try to wrap "console.error(error.message)" in {} like this :

request.on("error", (error) => {
console.error(error.message)
});

I know it should work without it but I noticed strange behaviours like this before so ...

I hope it'll help.

Raphaël Seguin
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,227 Points

Ooooops ! You should put the statement in the callback, where the request lives:

const request = https.get("https://teamtreehouse.com/chalkers.json", response => {
    console.log(response.statusCode);
    request.on("error", (error) => {
    console.error(error.message)
  });
});
David Sims
David Sims
857 Points

Just from first glance I noticed you're using $error.message instead of error.message, so it's going to return undefined instead of the error.

Javier MARQUEZ
Javier MARQUEZ
11,877 Points

Thanks a lot, for some reason when asking the question I left it like that, however if I try the following it still doesn't work. The error I got is "Cannot read property 'hasCallExpression' of undefined"

const https = require("https");

const request = https.get("https://teamtreehouse.com/chalkers.json", response => {
  console.log(response.statusCode);
});
request.on("error", (error) => console.error(error.message));
Ella Ruokokoski
Ella Ruokokoski
20,866 Points

I think you have forgotten to add curly braces around your callback function.

edit. oh Raphael was a tad faster :)