危険!私はMEANスタックが初めてです。間違った用語を使用している可能性があります。また、質問を明確にするために、例から多くのコードを削除してリファクタリングしました。
複数のプロパティを持つスキーマがあり、そのうちの 1 つは混合型です。その混合型は、別のスキーマによって定義されたサブドキュメントの配列です。この別のスキーマには、さらに別のスキーマによって定義された、さらに別のサブドキュメントの配列を含む、混合型のさらに別のプロパティがあります。
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]**
} ],
}
オブジェクト、あなたは言いますか?まあ、それはしません。そこで、検索を行って、「自分自身、historyEntry を mainStandard にプッシュしてから、値を mainStandard.history.values にプッシュしてみませんか?」
しかし、それもうまくいかないようです。ブラウザのコンソールに mainStandard.history.values が存在しないというエラーが表示されます。がっかりして、私はそれを印刷します。中括弧で引用された栄光の中で、私はそれをすぐそこに見ます!うーん...しかし、それはJavaScriptの名前付き配列ではないでしょうか? Object が上に表示される理由を説明します。
モデルを保存する前に、どのようにデータを入力できますか (これは間違った用語かもしれません)。