2

I have an Schema model like 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 i have the operations:

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

In that case, i'm getting a ValidationError because of the null value. This is expected. But, in the next operations:

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

Here the value is stored on database, and then i have an unexpected null value where it had been an array. Doing that with objects like this,

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

Also saves null values in the database that are prohibited to the data model.

I've read about validators, and middleware for checking things. Is there anyway to do this directly on the schema, or i have to parse the received object before i save it in the database? What is the best approach for this?

1 Answer 1

3

Well you could just use the model definitions for this. Even though you are embedding you can still do this but of course you do not want to actually save the objects to their own collection. Just feed them into the item as 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 what happens there is the validation will work for each stage, in this case it will fail on the name of the property schema item not being a string or even if not included.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick response! I've tried it, but the case where var obj = new ObjMod({ properties: null }); still inserts a null into the database. Any way to solve that?
@Sugarl3ss you can add other validation in that case. Just a problem where the null type does not fall into the the basic validation. But the point is if you structure like this then most of your problems should go away without writing loads of validation code.
Ok, then i will add some validators for that cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.