我有以下架构,当我要更新它时,我被提到异常。
我在这里看到了几个问题,但仍然有点不清楚我在哪里犯了错误。
模式
var DishSchema = new mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
dishes: [ {
name: { type: String, index: { unique: true } , required:true },
type: String,
descdish: String,
servingtype: String,
servingquantity: Number,
cuisine: String,
features: [ { type: String } ],
dishphotos: {
mainphoto: String,
secphoto: String,
thirdphoto: String
},
price: Number,
discount: {
offer: {type: Boolean , default: false },
discoutamount: String,
from: Date,
to: Date
},
availability: {
advancebooking: {
mindish: Number,
orderby: String
},
premake: {
from: Date,
to: Date,
aftertime: String,
orderby: String,
quantity: Number
},
regulardishes: [
{
weekday: String,
orderby: String,
readyby: String,
readyday: String,
pickfrom: String,
pickto: String,
qty: Number
}
]
}
} ]
});
在执行mongo语句之前,我将值存储在var / record中,如下所示。
var docId = req.body.userid;
var query = { user: docId };
var dishRecord = {
name : req.body.name || "" ,
type : req.body.type || "",
descdish : req.body.descdish || "",
servingtype : req.body.servingtype || "",
servingquantity : req.body.servingquantity || "",
cuisine : req.body.cuisine || "",
features : req.body.features || "",
dishphotos : req.body.dishphotos || "",
price : req.body.price || "",
discount : req.body.discount || "",
availability : {
advancebooking : req.body.availability.advancebooking || "",
premake : req.body.availability.premake || "",
regulardishes : req.body.availability.regulardishes || []
}
}
然后我跑
Dish.findByIdAndUpdate(
docId,
{ $push: {"dishes": dishRecord}},
{ safe: true, upsert: true},
function(err, model) {
if(err){
console.log("Dish DB Error::: ", err);
return res.send(err);
}
console.log( "Dish DB updated");
return res.json(model);
});
异常
Dish DB Error::: { [CastError: Cast to undefined failed for value "[object Object]" at path "dishes"]
stack: 'Error\n at MongooseError.CastError
at process._tickDomainCallback (node.js:381:11)',
message: 'Cast to undefined failed for value "[object Object]" at path "dishes"',
name: 'CastError',
kind: undefined,
value: [{"availability":{"regulardishes":[{"qty":"1","pickto":"","pickfrom":"","readyday":"","readyby":"","orderby":"","weekday":""},{"qty
":"1","pickto":"09:00 AM","pickfrom":"09:00 AM","readyday":"Same day","readyby":"09:00 AM","orderby":"09:00 AM","weekday":"Mon"}],"premake":
{"quantity":"1","orderby":"","aftertime":"","to":"09:00 AM","from":"09:00 AM"},"advancebooking":{"orderby":"12 Hours","mindish":"1"}},"disco
unt":{"to":"30-8-2015","from":"30-8-2015","discoutamount":"10%","offer":false},"price":"","dishphotos":{"thirdphoto":"","secphoto":"","mainp
hoto":""},"features":[],"cuisine":"","servingquantity":"1","servingtype":"","descdish":"","type":"","name":""}],
path: 'dishes' }