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 Error Handling

Nick Huemmer
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Nick Huemmer
Front End Web Development Techdegree Graduate 26,840 Points

My code is the same as Joel's, but for some reason I'm still getting an error when the form renders.

I added all the code below to app.js, but I still get submitted value is not a number and the stack trace - where else could I look to find what's gone awry?

app.use((req, res, next) => {
  if (req.body.number === undefined) {
    return next();
  }
  const num = parseFloat(req.body.number);
  if (isNaN(num)) {
    const err = new Error('Submitted value is not a number');
    return next(err);
  }
  const result = num * 2;
  req.doubled = result;
  next();
});

Even when I run the final provided code, it doesn't work. I wonder if something isn't set up on my machine correctly?

I don't know if it will help, but here's my stack trace:

Error: Submitted value is not a number
    at C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\app.js:23:17
    at Layer.handle [as handle_request] (C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\index.js:312:13)
    at C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\index.js:330:12)
    at next (C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\index.js:271:10)
    at serveStatic (C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\serve-static\index.js:74:16)
    at Layer.handle [as handle_request] (C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\index.js:312:13)
    at C:\Users\Nick\OneDrive\JavaScript TechDegree\javascript_understanding_express_middleware\javascript_understanding_express_middleware\05_error_handling_final\node_modules\express\lib\router\index.js:280:7

1 Answer

Reggie Williams
STAFF
Reggie Williams
Treehouse Teacher

Hey Nick Huemmer it seems like the error in the if statement that checks if the value is a number is being returned. Have you double checked that number is being assigned a number? I'd also say to try maybe try refreshing your cache