我有带有Itens数组的Encomenda。强度可以具有一系列的强度。 我有以下代码:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var idvalidator = require('mongoose-id-validator');
let ItemSchema = new Schema({
produtoId:Number,
itens:[{type:Schema.Types.ObjectId, ref: 'Item'}]
});
function autoPopulateItens(next){
this.populate('itens');
next();
}
ItemSchema
.pre('findOne',autoPopulateItens)
.pre('find',autoPopulateItens);
module.exports=mongoose.model('Item',ItemSchema);
let EncomendaSchema= new Schema({
itens:[ItemSchema]
});
module.exports=mongoose.model('Encomenda',EncomendaSchema);
function log(data){
console.log(JSON.stringify(data,undefined,2))
}
我正在尝试向邮递员发出发帖请求,以使用以下代码创建Encomenda:
{
"itens":[{
"produtoId":5,
"itens":[{
"produtoId":6,
"itens":[]
},{
"produtoId":7,
"itens":[]
},{
"produtoId":8,
"itens":[]
}]
}
]
}
但是当我尝试创建一个Encomenda并将其保存到mongodb时,它给了我一个错误:
CoreMongooseArray [ { itens: [], _id: 5bd9fde20c29e35f2c0ca74a, produtoId: 5 } ]
ValidationError: Encomenda validation failed: itens.0.itens: Cast to Array failed for value "[ { produtoId: 6 },
{ produtoId: 7, itens: [] },
{ produtoId: 8, itens: [] } ]" at path "itens"
at new ValidationError (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\error\validation.js:30:11)
at model.Document.invalidate (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:2026:32)
at EmbeddedDocument.invalidate (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\types\embedded.js:289:19)
at EmbeddedDocument.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:989:10)
at EmbeddedDocument._handleIndex (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:773:14)
at EmbeddedDocument.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:719:22)
at EmbeddedDocument.Document (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:115:12)
at EmbeddedDocument [as constructor] (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\types\embedded.js:39:12)
at new EmbeddedDocument (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\schema\documentarray.js:75:17)
at DocumentArray.cast (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\schema\documentarray.js:334:22)
at DocumentArray.SchemaType.applySetters (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\schematype.js:763:12)
at model.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:981:18)
at model._handleIndex (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:773:14)
at model.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:719:22)
at model.Document (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:115:12)
at model.Model (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\model.js:90:12)
这就是我尝试创建和保存Encomenda的方法:
exports.encomenda_create = function (req, res,next) {
var Client = require('node-rest-client').Client;
var client = new Client();
let encomenda=new Encomenda(req.body);
encomenda.save(function (err) {
if (err) {
return next(err);
}
res.send('encomenda criada com sucesso')
})
};
答案 0 :(得分:1)
将$json = json_decode($json, true);
作为参数传递给函数,如下所示:
next