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

cannot read properties of undefined

here is my code

products.js

const data = require('../data/products.json');
const products = data.products;

router.get('/', (req, res)=>{
    res.render('productslist');
});

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);
});

producttemplate.pug

extends layout

block content
    h1=productname
    p=productbio

data.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"
            }
        ]
    }
}

the app says "TypeError: Cannot read properties of undefined (reading '0')"

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,708 Points

Is this from a specific lesson or something else?

I recommend using camelCase for your variables, as it's rather tricky to scan/parse (productName vs. productname).