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
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter