Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add a subdocument to an extended schema #49

Open
Liron-K opened this issue Jun 29, 2016 · 0 comments
Open

Unable to add a subdocument to an extended schema #49

Liron-K opened this issue Jun 29, 2016 · 0 comments

Comments

@Liron-K
Copy link

Liron-K commented Jun 29, 2016

When I try and add the subdocument onto the extended schema, I get the following error:

{ [ValidationError: Car validation failed]
message: 'Car validation failed',
name: 'ValidationError',
errors:
{ engine:
{ [CastError: Cast to Embedded failed for value "{ _id: 57738e9cc1e7b9f5fe43ebaf, liters: 1.2 }" at path "engine"]
message: 'Cast to Embedded failed for value "{ _id: 57738e9cc1e7b9f5fe43ebaf, liters: 1.2 }" at path "engine"',
name: 'CastError',
kind: 'Embedded',
value: [Object],
path: 'engine',
reason: [TypeError: this.cast is not a function] } } }

Here is a test that i put together that replicates this.

var EngineSchema = new Schema({
  liters: {
    type:Number,
    default:1.2
  }
}, { safe : true });
var Engine = mongoose.model('Engine', EngineSchema);

var VehicleSchema = new Schema({
  name : String,
  engine: EngineSchema
}, { safe : true });
var Vehicle = mongoose.model('Vehicle', VehicleSchema);

var CarSchema = VehicleSchema.extend({
  doors : {
    type:Number,
    default:2
  }
});
var Car = mongoose.model('Car', CarSchema);

describe('Mongoose', function() {

  //This test succeeds
  it('can save with subdocument', function(done) {
    var vehicle = new Vehicle();
    chai.expect(vehicle.doors).to.equal(undefined);
    console.log("save vehicle");
    vehicle.name = "Name";
    vehicle.save()
    .then(function(savedVehicle){
      chai.expect(savedVehicle.name).to.equal("Name");
      chai.expect(savedVehicle.doors).to.equal(undefined);
      chai.expect(savedVehicle.engine).to.equal(undefined);

      var engine = new Engine();
      chai.expect(engine.liters).to.equal(1.2);

      vehicle.engine = engine;
      return vehicle.save();
    })
    .then(function(savedVehicle){
      chai.expect(savedVehicle.name).to.equal("Name");
      chai.expect(savedVehicle.doors).to.equal(undefined);
      chai.expect(savedVehicle.engine).to.not.equal(undefined);
      chai.expect(savedVehicle.engine.liters).to.equal(1.2);
      done();
    }, function(err){
      console.log(err);
    });
  });

  //This test fails
  it('should extend with subdocument', function(done) {
    var car = new Car();
    chai.expect(car.doors).to.equal(2);
    console.log("save car");
    car.save()
    .then(function(savedCar){
      chai.expect(savedCar.doors).to.equal(2);
      chai.expect(savedCar.engine).to.equal(undefined);

      var engine = new Engine();
      chai.expect(engine.liters).to.equal(1.2);

      car.engine = engine;
      return car.save();
    })
    .then(function(savedCar){
      chai.expect(savedCar.doors).to.equal(2);
      chai.expect(savedCar.engine).to.exist();
      chai.expect(savedCar.engine.liters).to.equal(1.2);

      done();
    }, function(err){
      console.log(err);
    });
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant