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 trialjason limmm
8,009 Points/products route not working
here is my new snapshot:https://w.trhou.se/hb1zqejqwl
my /products route isn't loading for some reason. i put off the project for a because i was busy. so i may not see the problem.
jason limmm
8,009 Pointsi can't install the package.json file for some reason and i moved all my pug files from the publics file to the empty views file
1 Answer
Rohald van Merode
Treehouse StaffHey jason limmm 👋
I just had a look at your updated snapshot and there are a couple of issues that are causing the errors you're encountering.
For the /products
page you'll want to have a look at what you're passing to the render
method. The second argument should be an object with the locals, instead you're currently passing it an array of products. Try converting it to an object with a products
key instead:
router.get('/', (req, res)=>{
res.render('productslist', {products});
});
You'll also want to have a look at your error handling as there are a couple of issues going on in there as well:
In your app.js
file you're referring to error
multiple times which does not exist in your project causing undefined
errors. First one being on line 25 where there is no error
object yet to pass the status of. You may want to create a new Error
here with a 404 status to act as a fallback for users trying to visit non existing pages. You can then pass this error down to the global error handler using next
instead of sending a response in this route. For example:
app.use((req, res, next)=>{
const error = new Error('This page can not be found');
error.status = 404;
next(error);
});
Then in your global error handler on line 30 you're passing error
to the render
method but this does not exist, instead you'll want to pass it the err
parameter. This is where thrown errors will end up 🙂
app.use((err, req, res, next)=>{
res.render('error', err);
});
After these changes your error handling should start working, you can test it by navigating to a non existing route like /noroute
.
Hope this helps you to get going again! I also highly recommend checking out our Express Basics Course which covers all these concepts 😃
Travis Alstrand
Treehouse Project ReviewerTravis Alstrand
Treehouse Project ReviewerI'm currently unable to get the project to run at all. Is there supposed to be a
package.json
file? I see thepackage-lock.json
but not the regular to install dependencies.At a glance, I believe the pug files should be in your
views
folder, as that is where they are looked for by default unless that is altered manually. But it's hard to tell without being able to test anything