Ошибка версии Mongoose - как это работает? - PullRequest
2 голосов
/ 08 марта 2019

Я пытаюсь понять ошибку версии.Каждый раз, когда я думаю, что у меня есть четкое понимание этого, я обнаруживаю, что все еще не в курсе.Может кто-нибудь помочь мне понять, почему спасение все еще работает?

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);
  });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...