I’m trying to retrieve a random value (id) in a dynamic way to as to be able to find a specific product in the future (I’m following a nodejs course from udemy and the instructor is building an online commerce webpage as an example). I’ve reviewed the code the instructor has written on the video over and over to make sure the error’s not on the syntaxis end, it clearly isn’t since all’s written exactly as it shows in the videos yet I keep on getting the error, if anyone can help please! I’ve got no clue what it could be since at this point I understand the error could be on the logic end of it, I don’t know if maybe I’m not understanding the logic well and therefore the mistake i’m making isn’t obvious to me yet.
I’ll give the community some of the code so you guys can give me a hdn, thank you all !
Error thrown in console:
TypeError: Cannot read property 'productId' of undefined
at exports.getProduct (C:UsersTOMASDesktopnode js MVCcontrollersshop.js:14:29)
at Layer.handle [as handle_request] (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterlayer.js:95:5)
at C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterindex.js:281:22
at param (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterindex.js:354:14)
at param (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterindex.js:365:14)
at Function.process_params (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterindex.js:410:3)
at next (C:UsersTOMASDesktopnode js MVCnode_modulesexpresslibrouterindex.js:275:10)
product.js file:
const fs = require('fs');
const path = require('path');
const p = path.join(
path.dirname(process.mainModule.filename),
'data',
'products.json'
);
const getProductsFromFile = cb =>{
fs.readFile(p, (err, fileContent) => {
if(err) {
cb([]);
} else{
cb(JSON.parse(fileContent));
}
});
}
module.exports = class Product {
constructor(title, imageUrl, description, price) {
this.title = title;
this.imageUrl = imageUrl;
this.description = description;
this.price = price;
}
save() {
this.id = Math.random().toString();
getProductsFromFile(products => {
products.push(this);
fs.writeFile(p, JSON.stringify(products), (err) => {
console.log(err);
});
});
}
static fetchAll(cb) {
getProductsFromFile(cb);
};
};
shop.js file:
const Product = require('../models/product');
exports.getProducts = (req, res, next) => {
Product.fetchAll(products => {
res.render('shop/product-list', {
prods: products,
pageTitle: 'All Products',
path: '/products'
});
});
};
exports.getProduct = (res, req, next) => {
const prodId = req.params.productId;
console.log(prodId);
res.redirect('/');
};
exports.getIndex = (req, res, next) => {
Product.fetchAll(products => {
res.render('shop/index', {
prods: products,
pageTitle: 'Shop',
path: '/'
});
});
};
exports.getCart = (req, res, next) => {
res.render('shop/cart', {
path: '/cart',
pageTitle: 'Your Cart'
});
};
exports.getOrders = (req, res, next) => {
res.render('shop/orders', {
path: '/orders',
pageTitle: 'Your Orders'
});
};
exports.getCheckout = (req, res, next) => {
res.render('shop/checkout', {
path: '/checkout',
pageTitle: 'Checkout'
});
};
product-list.ejs file:
<%- include('../includes/head.ejs') %>
<link rel="stylesheet" href="/css/products.css">
</head>
<body>
<%- include('../includes/navigation.ejs') %>
<main>
<% if (prods.length > 0) {%>
<div class="grid">
<div class="card">
<% for (let product of prods) { %>
<article class="product-item">
<header class="card__header">
<h1 class="product__title"> <%= product.title %> </h1>
</header>
<div class="card__image">
<img src="<%= product.imageUrl %>", alt="">
</div>
<div class="card__content">
<h2 class="product__price"> $<%= product.price %> </h2>
<p class="product__description"> <%= product.description %> </p>
</div>
<div class="card__actions">
<a href="/products/<%= product.id %>" class="btn">Details</a>
<form action="/add-to-cart" method="POST">
<button class="btn"> Add to Cart </button>
</form>
</div>
</article>
<% } %>
</div>
</div>
<% } else { %>
<h1>No Products</h1>
<% } %>
</main>
<%- include('../includes/end.ejs') %>
users.js file:
const path = require('path');
const express = require('express');
const shopController = require('../controllers/shop');
const router = express.Router();
router.get('/', shopController.getIndex);
router.get('/products', shopController.getProducts);
router.get('/products/:productId', shopController.getProduct);
router.get('/cart', shopController.getCart);
router.get('/orders', shopController.getOrders);
router.get('/checkout', shopController.getCheckout);
module.exports = router;