MEAN.js - How do you insert sub-documents into a mongoose model before saving? -


danger! i'm new mean stack. may using wrong terms. have removed , refactored lot of code example make question clearer.

i have schema multiple properties, 1 of of mixed type. mixed type array of subdocuments, defined schema. other schema has yet property of mixed type, containing yet array of subdocuments, defined yet 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 required field."             }             values: {                 type: [subsubschema],                 required: "value updates must contain values!"             } }) ;  var mainschema = new schema({     name: {         type: string,         trim: true,         default: '',         required: "name required field."     }     history: {         type: [subschema],         required: "must have history."     } });  mongoose.model('mainstandard', mainschema); 

in client side controller, create instance of mainstandard so:

mainstandard = new mainstandards({     name: $scope.name,     history: [] }); 

now create , insert history object...

// first create follows subschema var historyentry = {     name: "test",     values: [] };  // insert values view follow subsubschema  (factor = $scope.selection.length-1;factor>=0;factor--){       // store if selected      if ($scope.selection[factor].selected == true){           historyentry.values.push(              {factor: $scope.selection[factor].name, value: $scope.selection[factor].value}          );     } }  // insert finished historyentry... mainstandard.history.push(historyentry);  // ...and save finished mainstandard  mainstandard.$save(function(response) {         $location.path('main-standards/' + response._id);     }, function(errorresponse) {         $scope.error = errorresponse.data.message; }); 

when this, server side request looks this:

{ name: 'aoeu',   history:    [ {     created: 1433430622745,    creator: '55706a4ef725840d8e8d5716',    values: **[object]**   } ], } 

an object, say? wont do. searching , myself, "self, why don't push historyentry mainstandard, push values mainstandard.history.values?"

but doesn't seem work either. error in browser's console saying mainstandard.history.values doesn't exist. dismayed, print out. see right there, in curly braced, quoted glory! hmm... wouldn't javascript named array? explain why object shows above.

how can populate (this may wrong term use) model before save it?


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -