17

PHOTO EMBED

Fri Oct 28 2022 08:54:05 GMT+0000 (Coordinated Universal Time)

Saved by @Vrushabh_123

const { extend } = require('lodash')

router.route('/:productId').post(async (req, res) => {
  // get the product from the database
  try {
    const product = await Product.findById(id)
    if (!product) {
      return res
        .status(400)
        .json({ success: false, message: 'product not found' })
    }
  } catch {
    res
      .status(400)
      .json({ success: false, message: 'could not retrieve product ' })
  }

  // read the update from database
  const productUpdate = req.body

  // extend and save it
  product = extend(product, productUpdate) // saves time

  product.updated = Date.now()

  product = await product.save()

  res.json({ success: true, product })
})

// alternatively use findOneAndUpdate
content_copyCOPY