Related products

In each individual product view, we will show five related products as suggestions. To retrieve these related products, we will set up an API that accepts a request at /api/products/related.

mern-marketplace/server/routes/product.routes.js:

router.route('/api/products/related/:productId')              .get(productCtrl.listRelated)router.param('productId', productCtrl.productByID)

The :productId param in the route URL route will call the productByID controller method, which is similar to the shopByID controller method, and retrieves the product from the database and attaches it to the request object to be used in the next method.

mern-marketplace/server/controllers/product.controller.js:

const productByID = (req, res, next, id) => { Product.findById(id).populate('shop', ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.