2
votes

Working with mongoose and Express for a basic data endpoint, and I'm having trouble with the Update portion of the CRUD operations.

Testing the Update path works in Postman, but when I try from my angular app, it returns this:

MongoError: Updating the path '__v' would create a conflict at '__v' at C:\Users\rutherfordc.AA\Documents\GitHub\techInventory\node_modules\mongoose\node_modules\mongodb-core\lib\conne ction\pool.js:595:61 at authenticateStragglers (C:\Users\rutherfordc.AA\Documents\GitHub\techInventory\node_modules\mongoose\node_module s\mongodb-core\lib\connection\pool.js:513:16) at Connection.messageHandler (C:\Users\rutherfordc.AA\Documents\GitHub\techInventory\node_modules\mongoose\node_mod ules\mongodb-core\lib\connection\pool.js:549:5) at emitMessageHandler (C:\Users\rutherfordc.AA\Documents\GitHub\techInventory\node_modules\mongoose\node_modules\mo ngodb-core\lib\connection\connection.js:309:10) at Socket. (C:\Users\rutherfordc.AA\Documents\GitHub\techInventory\node_modules\mongoose\node_modules\mo ngodb-core\lib\connection\connection.js:452:17) at Socket.emit (events.js:160:13) at addChunk (_stream_readable.js:269:12) at readableAddChunk (_stream_readable.js:256:11) at Socket.Readable.push (_stream_readable.js:213:10) at TCP.onread (net.js:602:20)

I don't really want to update __v but I don't understand why it's being triggered. How can I force it to be ignored?

here's my update method:

  update(req,res){
    let _computer = req.body;
    let _id = req.params.computerId;
    Computer.findOneAndUpdate({'_id':_id}, _computer, {upsert: true}, (err, uc) => {
      if(err){
        log.error(err);
        res.status(500).send(err);
      }else{
        res.status(200).send(uc);
      }
    });
  }
2
Is __v being sent in the request body? - foxinatardis
yes, and I'm trying to either keep it from being sent to the client on the initial Read request, or delete it before sending the patch request from the client. - Chris Rutherford
Have you tried delete _computer.__v before calling findOneAndUpdate? You could also try setting up hooks on your find queries to exclude __v from the returned results so __v never gets to your front end if that applies to your situation. - foxinatardis
@foxinatardis The latter would be ideal. so on my read() and readOne() handlers just do delete obj.__v; before res.send()? - Chris Rutherford
You could do that. I was suggesting a middleware hook which would universally exclude it from find queries. See: mongoosejs.com/docs/middleware.html - foxinatardis

2 Answers

3
votes

You can do this to remove __v from sending by res.send()

just add '-__v' in Computer.findOneAndUpdate({'_id':_id},'-__v');

like

 update(req,res){
    let _computer = req.body;
    let _id = req.params.computerId;
    Computer.findOneAndUpdate({'_id':_id},'-__v', _computer, {upsert: true}, (err, uc) => {
      if(err){
        log.error(err);
        res.status(500).send(err);
      }else{
        res.status(200).send(uc);
      }
    });
  }

You can also show and hide any field in .find() and findById().

To hide use '-field_name1 , -field_name2'

like

Collection.find({},'-_id -__v');

and to show any specific field use 'field_name1 field_name2'

like

collection.find({},'name number');

0
votes

This issue has been fixed in mongoose@5.0.16. see this link for more details.

You can also delete __v from the req.body before updating:

if (req.body.__v) {
  Reflect.deleteProperty(req.body, '__v');
}