Arrays Mongoose架构:无法保存子文档
我正在尝试在mongodb中保存文档。没有任何成功 JSON:Arrays Mongoose架构:无法保存子文档,arrays,mongoose,Arrays,Mongoose,我正在尝试在mongodb中保存文档。没有任何成功 JSON: { "ticketType": "Enquiry", "type": "AA", "subType": "BB", "subSubType": "CC", "misidnProfile": { "serviceTypes": [ "Postpaid", "Prepaid" ], "circles": [
{
"ticketType": "Enquiry",
"type": "AA",
"subType": "BB",
"subSubType": "CC",
"misidnProfile": {
"serviceTypes": [
"Postpaid", "Prepaid"
],
"circles": [
"AA", "BB"
]
}
}
猫鼬模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var misidnProfile = new Schema({
serviceTypes: [String],
circles: [String],
});
var taggingProfile = new Schema({
ticketType: {type:String, required: true},
type: {type:String, required: true},
subType: {type:String, required: true},
subSubType: {type:String, required: true},
misidnProfile: {misidnProfile},
});
module.exports = mongoose.model("TaggingProfile", taggingProfile);
保存数据的代码:
module.exports.createTaggingProfile = function(req, res){
var taggingProfile = new TaggingProfile(req.body);
taggingProfile.save(function (err, tg) {
if(err){
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
res.status(200).json({
message: 'Saved TaggingProfile',
obj: tg
});
});
};
它正在保存标记配置文件,但不保存子文档msisdnProfile。我不明白问题出在哪里
编辑:
现在是储蓄。谢谢@Neil
在更新文档时,我将JSON作为
{
"ticketType": "Enquiry",
"type": "AA",
"subType": "BB",
"subSubType": "CC",
"misidnProfile": {
"serviceTypes": [
"Postpaid", "Prepaid", "ABC","PQR"
],
"circles": [
"AA", "BB"
]
}
}
奇怪的是,当我试图按id查找文档时,我得到的是TaggingProfile对象,但它没有msisdnProfile值。
检查下面的代码
TaggingProfile.findById(req.params.id, function (err, tg) {
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
if (!tg) {
return res.status(500).json({
title: 'No TaggingProfile Found!',
error: {message: 'Message not found'}
});
}
req.taggingProfile=tg;
if(tg.misidnProfile){
console.log("## tg.misidnProfile PRofile present");
} else {
console.log("## tg.misidnProfile Not present");
}
for(var p in req.body){
req.taggingProfile[p] = req.body[p];
}
req.taggingProfile.save(function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
res.status(200).json({
message: 'Updated message',
obj: result
});
});
});
misidnProfile:{misidnProfile},
应该是misidnProfile:misidnProfile,
您似乎在“培养”一种ES6对象分配技术,而实际上并不需要这种技术。@NeilLunn,感谢您的更新。它正在保存,但无法更新。现在我正在更新ServiceType或circles的值,但它没有更新。。缺少任何内容。显示实际提交的req.body
的内容。您发送给架构的数据的形状可能仍然不匹配says@NeilLunn请检查更新的问题