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,004 Points

unable 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey 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
jason limmm
8,004 Points
router.get('/:id', (req, res)=>{
    const productname = products[req.params.id].productname;
    const productbio = products[req.params.id].aboutme;
    const productinfo ={productname, productbio};
    res.locals.productinfo = productinfo;
    res.render('producttemplate');
});

i tried moving the render command down but now my localhost:3000/products isn't loading

Rohald van Merode
seal-mask
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

That'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
jason limmm
8,004 Points

is there a way for me to upload the file to the treehouse workspace?

Rohald van Merode
seal-mask
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

It 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 over products but the template doesn't know what products is, causing the undefined 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
jason limmm
8,004 Points

i tried adding the variable in both as an object and as a variable

router.get('/:id', (req, res)=>{
    const productname = products[req.params.id].productname;
    const productbio = products[req.params.id].aboutme;
    const productinfo ={productname, productbio};
    res.render('producttemplate', { productinfo});
});

but the /products route still isn't loading

Rohald van Merode
seal-mask
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

jason limmm you'll want to have a good look at what data you're working with. Your products variable looks like this:

{
      "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"
            }
      ]
}

So you'll want to make sure to use that req.params.id as an index on the products key, not on the object itself: const productName = products.products[req.params.id].productname

When in doubt, log it out

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 and productbio 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 🙂