May 2018
Intermediate to advanced
470 pages
13h 54m
English
We will update the product controller file to add the decreaseQuantity controller method, which will update the stock quantities of all the products purchased in the new order.
mern-marketplace/server/controllers/product.controller.js:
const decreaseQuantity = (req, res, next) => { let bulkOps = req.body.order.products.map((item) => { return { "updateOne": { "filter": { "_id": item.product._id } , "update": { "$inc": {"quantity": -item.quantity} } } } }) Product.bulkWrite(bulkOps, {}, (err, products) => { if(err){ return res.status(400).json({ error: "Could not update product" }) } next() })}
Since the update operation in this case involves a bulk update of multiple products in the collection after matching ...
Read now
Unlock full access