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:\Users\TOMAS\Desktop\node js MVC\controllers\shop.js:14:29)
at Layer.handle [as handle_request] (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\index.js:281:22
at param (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\index.js:354:14)
at param (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\index.js:410:3)
at next (C:\Users\TOMAS\Desktop\node js MVC\node_modules\express\lib\router\index.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 >
<div >
<% for (let product of prods) { %>
<article >
<header >
<h1 > <%= product.title %> </h1>
</header>
<div >
<img src="<%= product.imageUrl %>", alt="">
</div>
<div >
<h2 > $<%= product.price %> </h2>
<p > <%= product.description %> </p>
</div>
<div >
<a href="/products/<%= product.id %>" >Details</a>
<form action="/add-to-cart" method="POST">
<button > 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;
CodePudding user response:
In shop.js, you need to swap your req and res, your request comes first, then response, then next.
exports.getProduct = (req, res, next) => {
const prodId = req.params.productId;
console.log(prodId);
res.redirect('/');
};
CodePudding user response:
base of
at exports.getProduct (C:\Users\TOMAS\Desktop\node js MVC\controllers\shop.js:14:29)
you have error on shop.js line 14. means :
const prodId = req.params.productId;
req or params is undefined and
TypeError: Cannot read property 'productId' of undefined
