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

jason limmm
jason limmm
8,009 Points

error is not defined error

app.use((req, res, next)=>{
    const error = new Error('uh no!!!! no money for us!!!');
    res.status(error.status);
    next(error);
});

app.use((err, req, res, next)=>{
    res.locals.error = error;
    res.render('error', error);
});

the app says that 'error' is not defined i don't know what's the problem

1 Answer

Steven Parker
Steven Parker
231,265 Points

It's not clear what course and lesson you are referring to, but one issue stand out in the code itself. The first function defines "error", but its scope is local to that function only.

The second function tries to reference it (res.locals.error = error), but it would not be defined at that point.

jason limmm
jason limmm
8,009 Points

can i put the error variable outside both the functions so that it's not only local to one function

something like this

const error = new Error('uh no!!!! no money for us!!!');

app.use((req, res, next)=>{
    res.status(error.status);
    next(error);
});

app.use((err, req, res, next)=>{
    res.render('error', error);
});
Steven Parker
Steven Parker
231,265 Points

Anything declared in the global (outside) scope should be available to any function that doesn't shadow it (by having a local variable or parameter of the same name).