April 2020
Intermediate to advanced
716 pages
18h 55m
English
To allow users to select a specific category to search in, we will first set up an API that retrieves all the distinct categories present in the Products collection in the database. A GET request to /api/products/categories will return an array of unique categories, and this route is declared as shown here:
mern-marketplace/server/routes/product.routes.js:
router.route('/api/products/categories') .get(productCtrl.listCategories)
The listCategories controller method queries the Products collection with a distinct call against the category field, as shown in the following code:
mern-marketplace/server/controllers/product.controller.js:
const listCategories = async (req, res) => { try { let products = await Product.distinct('category',{}) ...Read now
Unlock full access