简体   繁体   English

Mongoose VersionError:保存文档时找不到匹配的ID文档

[英]Mongoose VersionError: No matching document found for id when document is being saved

I am repeatedly seeing the following error when synchronizing a users cart through a "/sync/" API request. 通过“ / sync /” API请求同步用户购物车时,我反复看到以下错误。 This is called whenever a user changes the contents of their shopping cart. 每当用户更改购物车的内容时,就会调用此方法。

VersionError: No matching document found for id "2y4b1hq601cd013e0af25e32" version 4 modifiedPaths "cart, cart.items, cart.updatedAt" at VersionError.MongooseError [as constructor] (/node_modules/mongoose/lib/error/mongooseError.js:13:11) at new VersionError (/node_modules/mongoose/lib/error/version.js:18:17) at generateVersionError (/node_modules/mongoose/lib/model.js:398:10) at model.Model.save (/node_modules/mongoose/lib/model.js:460:27) at /controllers/shoppingCart/index.js:48:14 at /node_modules/mongoose/lib/model.js:4670:16 at /node_modules/mongoose/lib/utils.js:258:16 at _hooks.execPost (/node_modules/mongoose/lib/query.js:4065:11) at /node_modules/kareem/index.js:135:16 at process._tickCallback (internal/process/next_tick.js:61:11) VersionError:找不到ID为“ 2y4b1hq601cd013e0af25e32”版本4的匹配文档,位于VersionError.MongooseError [as构造函数]处的路径“ cart,cart.items,cart.updatedAt”(/node_modules/mongoose/lib/error/mongooseError.js:13:11 )的新版本错误(/node_modules/mongoose/lib/error/version.js:18:17)的生成版本错误(/node_modules/mongoose/lib/model.js:398:10)的model.Model.save(/ node_modules /在/controllers/shoppingCart/index.js:48:14在/node_modules/mongoose/lib/model.js:4670:16在/ node_modules / mongoose / lib / utils处的mongoose / lib / model.js:460:27)。 _hooks.execPost(/node_modules/mongoose/lib/query.js:4065:11)处的js:258:16在/node_modules/kareem/index.js:135:16处的process._tickCallback(internal / process / next_tick.js) :61:11)

The exact code line is the following: 确切的代码行如下:

req.session.save();
delete user.__v;
>>      user.save();
return res.send();

I've tried user.increment() but this doesn't seem to fix this, nor deleting user.__v I am assuming this is conflicting versions, and I want versioning on my user objects, I just need to force the cart to always sync to the latest version. 我已经尝试过user.increment(),但这似乎不能解决此问题,也不能删除user .__ v。我假设这是相互冲突的版本,并且想要对用户对象进行版本控制,我只需要强制将购物车始终同步到最新版本。

While it would appear a .save() is the right approach here, an .update() command would get the job done while ignoring "race conditions" that cause this error to occur. 虽然看起来.save()是此处的正确方法,但.update()命令将完成工作,而忽略了导致此错误发生的“竞争条件”。 Mongo DB is throwing this error because it is concerned that I am saving an older version of the document that has already been updated: Mongo DB引发此错误,因为它担心我要保存已更新的旧版本文档:

  1. v1 is sent to client v1已发送给客户端
  2. v1 is saved, and updated to v2 in Mongo DB v1已保存,并在Mongo DB中更新到v2
  3. v1 is trying to be saved again, but Mongo DB already has v2 stored, error is thrown v1正在尝试再次保存,但是Mongo DB已经存储了v2,并引发错误

A better approach is to send v1 to the client and when the cart object changes, synchronize the object with the new cart object no matter what. 更好的方法是将v1发送到客户端,并且当购物车对象发生更改时,无论如何都将其与新的购物车对象同步。 This can be done via .update() rather than through .save() . 这可以通过.update()而不是.save()完成

This is because .save() watches and cares about version controls, while .update() will update the object regardless of version control. 这是因为.save() 监视并关心版本控制,而.update()会更新对象,而不考虑版本控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM