我有一个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' );
答案 0 :(得分:1)
删除文档版本,以解决版本冲突。 然后Mongoose将允许你保存:
delete item.__v
item.save(...)