Blank document and unhandled promise rejection when making an add subdocument request

Instead of fetching and then updating the document you can directly update the document in one DB call.

router.post("/update/:id",(req, res) =>{
    console.log(req.body.greek)
    var greek = req.body.greek;
    var english = req.body.english;
    var success = req.body.success;
    var timeStamp = req.body.timeStamp
    var newWord = {
        greek: greek,
        english: english,
        success: success,
        timeStamp: timeStamp
    }
    User.findOneAndUpdate({ _id: req.params.id }, {
            $push: {
                words: newWord
            }
        })
    .then((user) => {
      if(!user){
                return res.status(404).json({ 
                    message: 'Not User matches given ID'
                });
            }

            res.status(200).json(user);
    })
    .catch((err) => {res.status(400).json("Error: "+err)})
});

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top