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 Pointsunable to load the given route parameter
i am unable to load the given route parameter, i think it's because of the order i put my lines of code, if that's the case, i don't really know how to order the line of code.
product.js
const data = require('../data/products.json');
const products = data.products;
router.get('/', (req, res)=>{
res.render('productslist');
});
router.get('/:id', (req, res)=>{
res.render('producttemplate');
const productname = products[req.params.id].productname;
const productbio = products[req.params.id].aboutme;
const productinfo ={productname, productbio};
res.locals.productinfo = productinfo;
});
productslist.pug
extends layout
block content
h1 products here!!!!!!!
div.productlist
a(href="products/0").one phone 1
producttemplate.pug
extends layout
block content
h1=productinfo.productname
p=productinfo.productbio
products.json
{
"data": {
"title":"products",
"products":[
{
"productname":"aphone 14 pro makintos",
"aboutme":"i am versatile makapplecomp for the 1941 to now i still been used because of my uh versatile"
},
{
"productname":"phone",
"aboutme":"idk"
}
]
}
}
1 Answer
Rohald van Merode
Treehouse StaffHey jason limmm 👋
You'll want to make sure to add all the logic you need for your template before rendering it to the user. Moving down the res.render()
call to the very end of your route should fix the issue you're experiencing.
Hope this helps!
jason limmm
8,009 Pointsjason limmm
8,009 Pointsi tried moving the render command down but now my localhost:3000/products isn't loading
Rohald van Merode
Treehouse StaffRohald van Merode
Treehouse StaffThat's interesting, generally speaking a change in one route shouldn't affect the behavior of another. It would be helpful seeing your code in its entirety, either through a workspace or GitHub repo, this way we'll be able to run it locally to get a better idea of what is going on. With just these snippets it's hard to tell what is wrong as we're missing some context and are unable to test or check for errors.
jason limmm
8,009 Pointsjason limmm
8,009 Pointsis there a way for me to upload the file to the treehouse workspace?
jason limmm
8,009 Pointsjason limmm
8,009 Pointsok i managed to make my project: https://w.trhou.se/r5ry71xi48
Rohald van Merode
Treehouse StaffRohald van Merode
Treehouse StaffIt looks like you've made adjustments to your
productslist.pug
template too after sharing the snippets above which causes the error you're running into. In this template you're trying to loop overproducts
but the template doesn't know whatproducts
is, causing theundefined
error. You'll want to make sure to pass this variable down in the render method call in order to get access to its value in the template.You can refer to this video about the Response.render method on how to do this 🙂
jason limmm
8,009 Pointsjason limmm
8,009 Pointsi tried adding the variable in both as an object and as a variable
but the /products route still isn't loading
Rohald van Merode
Treehouse StaffRohald van Merode
Treehouse Staffjason limmm you'll want to have a good look at what data you're working with. Your
products
variable looks like this:So you'll want to make sure to use that
req.params.id
as an index on theproducts
key, not on the object itself:const productName = products.products[req.params.id].productname
When you're not sure about a certain error or value or why something is happening, try logging some or your variables to the console. Logging
productname
andproductbio
would tell you that they are both undefined from where you can do further debugging 🙂 Logging values to the console can be a simple and efficient way to see what your code is doing and if that meets what you're expecting it to do 🙂