Danger! I'm new to the MEAN stack. I may be using the wrong terms. I have also removed and refactored a lot of code from my example to make my question clearer.
I have a schema with multiple properties, one of which is of mixed type. That mixed type is an array of subdocuments, defined by another schema. This other schema has yet another property of mixed type, containing a yet another array of subdocuments, which is defined by yet another schema.
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);
In my client side controller, I create an instance of MainStandard like so:
mainStandard = new MainStandards({
name: $scope.name,
history: []
});
Now I would like to create and insert the history object...
// 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;
});
When I do this, the server side request looks like this:
{ name: 'aoeu',
history:
[ {
created: 1433430622745,
creator: '55706a4ef725840d8e8d5716',
values: **[Object]**
} ],
}
An object, you say? Well that just wont do. So I do some searching and say to myself, "Self, why don't you push the historyEntry into the mainStandard, then push the values to mainStandard.history.values?"
But that doesn't seem to work either. I get an error in my browser's console saying that mainStandard.history.values doesn't exist. Dismayed, I print it out. I see it right there, in its curly braced, quoted glory! Hmm... but wouldn't that be a JavaScript named array? It would explain why Object shows up above.
How can I populate (this may be the wrong term to use) my model before I save it?