April 2020
Intermediate to advanced
716 pages
18h 55m
English
In order to implement the backend API to retrieve products from a specific shop in the database, we will set up a GET route at /api/products/by/:shopId, as shown in the following code:
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. The listByShop method is defined as shown in the following code:
mern-marketplace/server/controllers/product.controller.js:
const listByShop = async (req, res) => { try { let products = await Product.find({shop: req.shop._id}) .populate('shop', '_id name').select('-image') ...Read now
Unlock full access