Mongoose VersionError:保存文档时找不到与 id 匹配的文档 [英] Mongoose VersionError: No matching document found for id when document is being saved

查看:21
本文介绍了Mongoose VersionError:保存文档时找不到与 id 匹配的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过/sync/"API 请求同步用户购物车时,我反复看到以下错误.每当用户更改购物车的内容时都会调用此方法.

I am repeatedly seeing the following error when synchronizing a users cart through a "/sync/" API request. This is called whenever a user changes the contents of their shopping cart.

VersionError: 没有找到与 id 匹配的文档2y4b1hq601cd013e0af25e32"第4版修改路径cart,cart.items,购物车.updatedAt"在 VersionError.MongooseError [作为构造函数] (/node_modules/mongoose/lib/error/mongooseError.js:13:11)在新版本错误 (/node_modules/mongoose/lib/error/version.js:18:17)在 generateVersionError (/node_modules/mongoose/lib/model.js:398:10)在model.Model.save (/node_modules/mongoose/lib/model.js:460:27)在/controllers/shoppingCart/index.js:48:14在/node_modules/mongoose/lib/model.js:4670:16在/node_modules/mongoose/lib/utils.js:258:16在 _hooks.execPost (/node_modules/mongoose/lib/query.js:4065:11)在/node_modules/kareem/index.js:135:16在 process._tickCallback (internal/process/next_tick.js:61:11)

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)

确切的代码行如下:

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

我试过 user.increment() 但这似乎不能解决这个问题,也不能删除 user.__v 我假设这是版本冲突,我想对我的用户对象进行版本控制,我只需要强制购物车始终同步到最新版本.

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.

推荐答案

虽然看起来 .save() 是正确的方法,但 .update()> 命令将完成工作,同时忽略导致此错误发生的竞争条件".Mongo DB 抛出此错误是因为它担心我正在保存已更新的旧版本文档:

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. 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:

  1. v1 发送给客户端
  2. v1 已保存,并在 Mongo DB 中更新为 v2
  3. v1 正在尝试再次保存,但 Mongo DB 已经存储了 v2,抛出错误

更好的方法是将 v1 发送给客户端,当购物车对象发生变化时,无论如何都要将对象与新的购物车对象同步.这可以通过 .update() 而不是通过 .save() 来完成.

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. This can be done via .update() rather than through .save().

这是因为.save()关注并关注版本控制,而.update()会更新对象而不管版本控制.

This is because .save() watches and cares about version controls, while .update() will update the object regardless of version control.

这篇关于Mongoose VersionError:保存文档时找不到与 id 匹配的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆