How do I make a PUT request with express and Postman in order to change a given MongoDB field?

I recommend you to split up the operations(find at first, then update, then save) Try something like this in callback of your put handler:

const updates = Object.keys(req.body)
try {
        const account = await Account.findById(req.params.id)
    
        updates.forEach((update) => account[update] = req.body[update])
        await account.save()
    
        if (!account) {
            return res.status(404).send()
        }
    
        res.send(account)
    } catch (e) {
        res.status(400).send(e)
    }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top