Read a shop API

In the backend, we will add a GET route that queries the Shop collection with an ID and returns the shop in the response.

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

router.route('/api/shop/:shopId')    .get(shopCtrl.read)router.param('shopId', shopCtrl.shopByID)

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

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

const shopByID = (req, res, next, id) => {  Shop.findById(id).populate('owner', '_id name').exec((err, shop) => {    if (err || !shop)      return res.status('400').json({ error: "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.