【问题标题】:VersionError: No matching document found error only on (Mocha) testVersionError: No matching document found 仅在 (Mocha) 测试中出现错误
【发布时间】:2014-12-20 16:18:12
【问题描述】:

我已经阅读了人们在使用版本密钥时遇到的其他问题/答案,但由于某种原因,我不明白为什么在我的情况下会发生这种情况。

所以我有以下摩卡测试:

    it('should be able to save one job', function (done) {

        Promise.join(user.saveAsync(), company.saveAsync(),
            function (savedUser, savedCompany) {
                user = savedUser[0];
                user.jobs.push(job);
                user.saveAsync()
                    .spread(function (savedUserWithJob) {
                        user = savedUserWithJob;
                        user.jobs.should.have.length(1);
                        done();
                    })
                .catch(function (err) {
                  done(err);
                });
        });

    });

这一切都很好并且通过了。即使在运行时我也没有任何其他问题。

现在,当我尝试在第一个测试之后再次运行相同的测试时,即:

    it('should be able to save one job', function (done) {
       .....
    });

    it('should be able to save one job again', function (done) {
       .....
    });

第二个失败并出现错误:

版本错误:找不到匹配的文档。

我真的不明白为什么会发生这种情况,因为我已经第一次推送到阵列而没有版本控制问题。为什么连续第二次失败?

【问题讨论】:

    标签: node.js mongoose mocha.js bluebird


    【解决方案1】:

    Mongoose versionKey在文档中保存一个版本号,通常命名为__v

    只要对数组的修改可能会更改任何数组的元素位置,该值就会自动递增。对于需要使用位置符号的任何更新,该值也会在 where 子句中发送。如果我们的 where 子句仍然匹配文档,它确保没有其他操作改变我们的数组元素位置,并且可以使用位置语法。 (read more here)

    在您的测试中,在第一个 save() 之后,文档有一个递增的 __v,应该在您的文档上更新,以便猫鼬在第二个 save() 上将其用作 where clause 的一部分。

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 2021-08-26
      • 1970-01-01
      • 2019-05-07
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      相关资源
      最近更新 更多