3

I am trying to understand the version error. Every time I think I have a firm understanding of it, I find out I'm still off base. Can someone help me understand why resaving would still work?

context('when dealing with multiple updates to the same thing', () => {
  let thing;
  let thingA;
  let thingB;
  before(async () => {
    thing = utils.generateThing();
    await thing.save();
    // Get the thing directly from the database (same thing, different object)
    thingA = await models.Thing.findOne({ '_id': thing._id });
    thingB = await models.Thing.findOne({ '_id': thing._id });
  });

  it('should handle the update', async () => {
    let yupItSaved = false;
    // Save modified thing to database (bumps the version)
    thingA.set('propertyArray', ['Monday', 'Tuesday']);
    await thingA.save();

    // Then try and save thing object
    thingB.set('propertyArray', ['Monday', 'Tuesday']);

    try {
      thingB.__v.should.equal(0);
      thingA.__v.should.equal(1);
      await thingB.save();
      should.fail(null, null, 'VersionError was not thrown');
    } catch (err) {
      // Expect the VersionError here since versions don't match
      err.name.should.equal('VersionError');
      const thingAfterError = await models.Thing.findOne({ '_id': thing._id });
      thingAfterError.__v.should.equal(1);
      thingA.__v.should.equal(1);
      thingB.__v.should.equal(0);
      // Don't understand why this works even though versions still don't match
      await thingB.save();
      yupItSaved = true;
    }
    yupItSaved.should.equal(true);
  });
});
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
loctrice
  • 2,454
  • 1
  • 23
  • 34
  • 1
    `mongoose.set("debug", true)` and watch the issued queries/updates. Using `save()` is a bad pattern anyway. Instead of `find()` then "alter in code" then `save()` you should always be using the atomic update modifiers of MongoDB. Trusting external mechanisms like this is bound for failure. – Neil Lunn Mar 08 '19 at 21:06
  • I appreciate your comment, and I've realized in the short time that I've been working with this code that save is bad. However I'm not really in a good position to change it. – loctrice Mar 10 '19 at 18:20
  • Really? What exactly do you think is so difficult to change? [Mongoose difference between .save() and using update()](https://stackoverflow.com/q/22278761/2313887). Point is that you're probably *"Barking up the wrong tree!"* as the saying goes. The solution isn't *"Make versions work"*, but instead to realize how atomic modifiers make such things unnecessary. If not altogether dangerous not to take the time to understand these things. – Neil Lunn Mar 10 '19 at 22:20
  • Yes really. Your reply seems to imply either a) I'm writing this code from scratch and can just write it the way I want or b) you have great insight into the application I'm working with to be able to say the change is trivial. – loctrice Mar 11 '19 at 11:19
  • 1
    @NeilLunn - The mongoose documentation seems to drive folks towards using `save`: "The save() function is generally the right way to update a document with Mongoose." (https://mongoosejs.com/docs/documents.html#updating). Also, in cases where there is save middleware atomic updates won't cause those to execute, correct? – MikeTheReader Mar 12 '19 at 14:38

0 Answers0