猫鼬-版本错误:找不到与ID相符的文件 [英] Mongoose - Version Error: No matching document found for id

查看:86
本文介绍了猫鼬-版本错误:找不到与ID相符的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我有一个Post猫鼬模型,该模型包含一个用于存储csv字符串的csv_files数组字段.我从另一个Web应用程序发出了fetch API请求,以发布特定Post的csv字符串.

Context: I have a Post Mongoose model that contains a csv_files array field to store csv strings. I make a fetch API request from a different web app to POST the csv strings for a particular Post.

下面的代码会产生错误

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:2):VersionError:找不到ID为"5966932399e969ad8013bedf"的匹配文档

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): VersionError: No matching document found for id "5966932399e969ad8013bedf"

router.js

router.post('/posts/:url/upload-csv/:csv_name', (req, res) => {
  let csv_name = req.params.csv_name;
  let csv_string = csv_name+req.body.csv_string;

  Post.findOne({url: req.params.url})
    .then((post) => {
      if (post.csv_files.length === 0) {
        post.csv_files.push(csv_string);
      } else {
        let foundExistingCSV = false;
        for (var i = 0; i < post.csv_files.length; i++) {
          if (post.csv_files[i].includes(csv_name)) {
            foundExistingCSV = true;
            post.csv_files[i] = csv_string;
            break;
          }
        }
        if (!foundExistingCSV) post.csv_files.push(csv_string);
      }

      post.markModified('csv_files');
      post.save();

      return res.status(200);
    })
    .catch((err) => {
      console.log(err);
      return res.status(400);
    });
});

models/post.js

const mongoose    = require('mongoose');

var stringPreset = {
  type: String,
  uppercase: true
}

var Post = mongoose.model('Post', {
  name                 : stringPreset,
  url                  : String,
  password             : String,
  csv_files            : { type : Array , "default" : [] },
  updatedAt            : { type: Date, default: Date.now }
});

module.exports = { Post };

如何解决此问题,以便文档可以正确地保存到csv_files数组中?

How can I fix this so that the document saves into the csv_files array without the error?

推荐答案

将对象保存到Mongo DB时,您必须了解Mongo DB具有适当的版本控制系统.这有助于确保如果保存一次对象,则再次保存时不会导致覆盖先前保存的数据.

When saving an object to Mongo DB you have to understand that Mongo DB has a version control system in place. This helps ensure that if you save an object once, when saving it again you don't end up overwriting the previously saved data.

这是您看到的错误.如果要在此特定实例中不管版本控制如何强制更新对象,则可能要使用 .update().这将强制更新对象,无论其当前保存状态如何.

This is the error you're seeing. If you want to force the object to update regardless of version control in this particular instance you may want to use .update() instead. This will force the object to be updated regardless of its currently saved state.

这是因为 .save()会监视并关心版本控制,而 .update()会更新对象,而不考虑版本控制.

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

这篇关于猫鼬-版本错误:找不到与ID相符的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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