To retrieve products from a specific shop in the database, we will set up a GET route at /api/products/by/:shopId, as follows.
mern-marketplace/server/routes/product.routes.js:
router.route('/api/products/by/:shopId') .get(productCtrl.listByShop)
The listByShop controller method executed in response to this request will query the Product collection to return the products matching the given shop's reference.
mern-marketplace/server/controllers/product.controller.js:
const listByShop = (req, res) => { Product.find({shop: req.shop._id}, (err, products) => { if (err) { return res.status(400).json({ error: errorHandler.getErrorMessage(err) }) } res.json(products) }).populate('shop', '_id name').select('-image')}
In the frontend, ...