猫鼬版本错误-这是如何工作的?

时间:2019-03-08 19:39:07

标签: mongoose

我正在尝试了解版本错误。每当我认为自己对此有深刻的了解时,我就会发现自己仍处于失控状态。有人可以帮助我了解为什么重新保存仍然有效吗?

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);
  });
});

0 个答案:

没有答案