危险!我是MEAN堆栈的新手。我可能使用了错误的条款。我也从我的例子中删除并重构了很多代码,以使我的问题更加清晰。
我有一个包含多个属性的模式,其中一个属性为mixed type。混合类型是由另一个模式定义的子文档数组。这个其他模式还有另一个混合类型的属性,包含另一个子文档数组,它由另一个模式定义。
var SubSubSchema = new Schema({
name: { type: String, required: true },
value: { type: Number, required: true }
});
var SubSchema = new Schema({
name: {
type: String,
trim: true,
default: '',
required: "Name is a required field."
}
values: {
type: [SubSubSchema],
required: "Value updates must contain values!"
}
}) ;
var MainSchema = new Schema({
name: {
type: String,
trim: true,
default: '',
required: "Name is a required field."
}
history: {
type: [SubSchema],
required: "Must have a history."
}
});
mongoose.model('MainStandard', MainSchema);
在我的客户端控制器中,我创建了一个MainStandard实例,如下所示:
mainStandard = new MainStandards({
name: $scope.name,
history: []
});
现在我想创建并插入历史对象......
// First create something that follows the SubSchema
var historyEntry = {
name: "test",
values: []
};
// Now insert the values from the view that follow the SubSubSchema
for (factor = $scope.selection.length-1;factor>=0;factor--){
// only store if selected
if ($scope.selection[factor].selected == true){
historyEntry.values.push(
{factor: $scope.selection[factor].name, value: $scope.selection[factor].value}
);
}
}
// Insert the finished historyEntry...
mainStandard.history.push(historyEntry);
// ...and save the finished mainStandard
mainStandard.$save(function(response) {
$location.path('main-standards/' + response._id);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
当我这样做时,服务器端请求如下所示:
{ name: 'aoeu',
history:
[ {
created: 1433430622745,
creator: '55706a4ef725840d8e8d5716',
values: **[Object]**
} ],
}
你说的一个对象?好吧,那就是不行。所以我做了一些searching并对自己说,&#34; Self,为什么不将historyEntry推入mainStandard,然后将值推送到mainStandard.history.values?&#34; < / p>
但这似乎也无济于事。我的浏览器控制台出现错误,说mainStandard.history.values不存在。沮丧,我把它打印出来。我看到它就在那里,用它的花括号,引用荣耀!嗯......但是这不是一个名为JavaScript的数组吗?它可以解释为什么Object出现在上面。
在保存之前,如何填充(这可能是错误的术语)我的模型?