node.js - Mongoose null checking before saving on nested schema array -


i have schema model this:

var propertyschema = new schema({   name: {type: string, required: true},   surname: string });  var objschema = new schema({   properties: [prepertyschema] });  var accountschema = new schema({   objects: [objschema] });  mongoose.model('account', accountschema); 

then have operations:

account.objects.push(null); account.save(function(error, account) {    //error checking , response }) 

in case, i'm getting validationerror because of null value. expected. but, in next operations:

var obj = {properties: null} account.objects.push(obj); account.save(function(error, account) {    //error checking , response }) 

here value stored on database, , have unexpected null value had been array. doing objects this,

var obj = {    properties: [{name:'randname'}, null] } 

also saves null values in database prohibited data model.

i've read validators, , middleware checking things. there anyway directly on schema, or have parse received object before save in database? best approach this?

well use model definitions this. though embedding can still of course not want save objects own collection. feed them item embedded:

  var async = require("async"),       mongoose = require("mongoose"),       schema = mongoose.schema;    mongoose.connect('mongodb://localhost/prop');    var propertyschema = new schema({       name: { type: string, required: true },       surname: string   });    var objschema = new schema({       properties: [propertyschema],   });    var accountschema = new schema({       objects: [objschema]   });    var account = mongoose.model( 'account', accountschema );   var objmod = mongoose.model( 'objmod', objschema, null, false );   var propmod = mongoose.model( 'propmod', propertyschema, null, false );    var account = new account();    var prop = new propmod({ "name": null  });   var obj = new objmod({ properties: prop });   account.objects.push( obj );    account.save(function(err,account) {       if (err) throw err;        console.log( json.stringify( account, undefined, 4 ) );    }); 

so happens there validation work each stage, in case fail on name of property schema item not being string or if not included.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -