node.js - Mongoose 版本错误且未找到 id 的匹配文档

标签 node.js mongoose

我有一个 Mongoose 模式,我刚刚在原始模式中添加了“imagens”字段。

添加的字段是:

imagens:[{title:{type: String},savedAs:{type: String},file:{type: String}, thumb:{type: String}}],

现在,当我尝试填写“imagens”字段并更新集合时,我收到下一个错误。 如果我去掉“item.imagens=imgs”行,错误就会消失。

我做错了什么?这个问题有解决办法吗?

//错误

     {"data":{"message":"No matching document found for id \"5909caeed32a453b537f7966\"",
"name":"VersionError"},    "status":500,
        "config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback",
    "url":"/uploads",
    "data":{"file":{"$ngfBlobUrl":
    "blob:http://localhost/9c4b0449-1ddd-4e39-ab44-f2e9a21bfd82","$ngfWidth":450,"$ngfHeight":321,
    "upload":{},"progress":100},"pacID":"5909caeed32a453b537f7966"},"_isDigested":true,
    "_chunkSize":null,"headers":{"Accept":"application/json, text/plain, */*"},"_deferred":{"promise":"..."},"cached":false},"statusText":"Internal Server Error"}

//在我的路由器中

Cliente.findById(pac_id, function (err, item) {  
                                    if (err) {
                                        return res.status(500).send(err);
                                    } else {
                                       item.imagens=imgs
                                    }
                                    item.save(function (err, data) {
                                      if (err) {
                                            return res.status(500).send(err)
                                      }
                                      if (answers.results.length){
                                            answers.message='Some files was not uploaded'
                                      } else {
                                            answers.message='Files were uploaded'
                                      }
                                      res.send(answers)  
                                   })
                                  })

//型号

const mongoose=require('mongoose');
const clientesSchema = new mongoose.Schema({
  id: {type: Number, unique:true},
  nome: {type: String, unique:true},
  ativo: {type: Boolean},
  ...
  ...  
  foto: { data: Buffer, contentType: String },
  imagens:[{title:{type: String},savedAs:{type: String},file:{type: String}, thumb:{type: String}}],
  created_at:{type:Date,default:Date.now},
  altered_at:{type:Date,default:Date.now}
});


module.exports = mongoose.model('Cliente', clientesSchema,'clientes' );

最佳答案

删除文档版本,即可解决版本冲突。 然后 Mongoose 将允许你保存:

delete item.__v
item.save(...)

关于node.js - Mongoose 版本错误且未找到 id 的匹配文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46281386/

相关文章:

node.js - Mongoose方法返回id

javascript - 如何在运行 Mongodb 的 Mongoose 中执行脚本?

node.js - Azure 网站 node.js http 请求失败

javascript - kafka-node 有没有办法在崩溃后请求收到的消息?

node.js - 在 laravel 中使用 Firebase-admin 时找不到依赖项

javascript - 如何仅针对 hasOwnProperty false 属性将 Object.assign() 默认值分配给另一个部分完整的对象?

javascript - Expressjs : mysql row object sent as variable to pug view's javascript prints as undefined

node.js - 登录时未定义的属性

node.js - 如何编辑 Mongoose 中的嵌套对象

javascript - Mongoose 和 NodeJS : Creating a unique system-generated username