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 HTTP Methods and Headers Sending Content Type Headers in Node.js

James Barrett
James Barrett
13,253 Points

How is the error message being displayed?

Hi guys,

    studentProfile.on("error", (error) => {
      renderer.view("error", {}, res);
      renderer.view("search", {}, res);
      renderer.view("footer", {}, res);
      res.end();
    });

Currently the error message from error.html is displaying this: https://gyazo.com/e208718b5b35affc9f5966c9ec391bf2

This might be something I missed in a previous video. What values do I send through to the 'view' function and how would it work?

Thanks, James.

3 Answers

John Knotts
seal-mask
.a{fill-rule:evenodd;}techdegree
John Knotts
Full Stack JavaScript Techdegree Student 10,836 Points

I had the same issue. I fixed it by changing the code to this:

    // on "error"
    studentProfile.on("error", function(error) {
      // show error
      renderer.view("error", {values:{errorMessage: error.message}}, response);
      renderer.view("search", {}, response);
      renderer.view("footer", {}, response);
      response.end();
    });

and part of that issue was because I had to change part of my code in renderer.js to the below to get one of the earlier sections to work:

function view(templateName, values, response) {
  // Read from the template file
  let fileContents = fs.readFileSync('./views/' + templateName + '.html', {encoding: "utf8"});
  // Insert values into the content
  fileContents = mergeValues(values.values, fileContents);
  // Write out the contents to the response
  response.write(fileContents);
}

note the values.values argument... I don't know why, maybe because I'm working on it locally on my own machine, but that's what I had to do to access the object properly.

C McGowan
C McGowan
6,866 Points

Hey James,

The reason is because in your below method you aren't passing in any values.

renderer.view("error", {}. function);

If you aren't passing in any values, your mergeValues function ( also found in renderer.js) is not going to cycle over the content to replace {{errorMessage}} in the content body, which is why your error template is just displaying the placeholder.

Try passing your renderer function {errorMessage: error.message} and see if that works.

I had the same problem. I console logged the "key" right before the for in loop when I submitted a request that should prompt the appropriate error message. It showed up as "errorMessage".

So, I went and changed the html to this:

<div id="error">{{errorMessage}}</div>

The logic is set up to find whatever "key" is and replace it with the error.message. That worked for me.