[Vuejs]-.save is not a function, and .findOneAndUpdate() doesn't update my db

0πŸ‘

βœ…

In your first example it looks like you are finding with the wrong param, and you should be using id.

Try using findByIdAndUpdate instead:

routes.put('/:id', (req, res) => {
 mongoose.set('useFindAndModify', false);
  PC.findByIdAndUpdate( 
    req.params.id,
    { '$set': {'description': req.body.description, 'title': req.body.title} },
    {'new': true },
    function(err, pc) {
        if (err) {
          console.log('ERROR WHILE PUT PC');
            throw (err);
        } else {
            console.log('Succes set');
            res.status(200).send(pc)
        }
    }
  );
})

In you second example, you should be calling .save on the result, not the original PC Model. You could change that to:

routes.put('/:id', (req, res) => {
  PC.findById(req.params.id, 'title description', function (error, pc) {
    if (error) { console.error(error); }

    // Use lowercase `pc` on all these lines
    pc.title = req.body.title
    pc.description = req.body.description
    console.log(pc);
    pc.save(function (error) {
      if (error) {
        console.log(error)
      }
      res.send({
        success: true,
        message: 'PC saved successfully!',
        PC: req.body
      })
    })
  })
})

Leave a comment