Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose embedded docs master detail callback sequence

I try to do some operation in the pre save hook of a child item but it never seems to get fired. I only get the pre save hook of the master fired.

My models in Javascript

    var mongoose = require('mongoose');
mongoose.connect("localhost","test_master_details");

var masterSchema = mongoose.Schema({
  title:{type:String,required:true},
  childs: [childSchema]
});

masterSchema.pre('save',function(next){
  console.log("master pre save called!");
  console.log(this);
  next();
});

var childSchema = mongoose.Schema({
  number:{type:Number},
  name: {type:String,required:true}

});

childSchema.pre('save',function(next){
  this.number = 1;
  //set number with the total number of childs for the parent master
  console.log('child pre save called!');
  console.log(this);
  next();
});



module.exports = [mongoose.model('Child',childSchema), mongoose.model('Master',masterSchema)];

my specs in coffee-script:

 model_container = require('../models/master')

Master = model_container[1]
Child = model_container[0]
should = require('should')

describe "Master model",->
  before (done) ->
    Master.remove {},(err) ->
      console.log err if err
      done()
  it "should be able to create an instance",(done)->
    master = new Master {title:"Master of command"}
    master.save (err) ->
      console.log err if err
      should.exist master
      done()
  it "should be able to add 1 child", (done)->
    master= new Master {title:"Master of command"}
    master.childs.push {name:"hello world"}
    master.save (err) ->
      console.log err if err
      master.childs.length.should.eql 1
      done()
  it "should be able to add 1 child with create method",(done) ->
    master = new Master {title:"Master of command"}
    child = new Child {name:"childs name"}
    master.childs.push child
    master.save (err) ->
      console.log err if err
      master.childs.length.should.eql 1
      done()

Here are my spec results childs pre-save is never called:

 Master model
    ◦ should be able to create an instance: master pre save called!
{ title: 'Master of command',
  _id: 50322715672b424e11000001,
  childs: [] }
    ✓ should be able to create an instance 
    ◦ should be able to add 1 child: master pre save called!
{ title: 'Master of command',
  _id: 50322715672b424e11000002,
  childs: [ { name: 'hello world' } ] }
    ✓ should be able to add 1 child 
    ◦ should be able to add 1 child with create method: master pre save called!
{ title: 'Master of command',
  _id: 50322715672b424e11000003,
  childs: [ { name: 'childs name', _id: 50322715672b424e11000004 } ] }
    ✓ should be able to add 1 child with create method 


  ✔ 3 tests complete (20ms)

Am I missing something? From my understanding should the child pre save fire whenever I add a child to the collection and save it afterwords. What do I do wrong? I can I add a child to a masters collection and pre set values based on masters values?

Thanks for any help...

like image 700
silverfighter Avatar asked Sep 03 '26 00:09

silverfighter


1 Answers

You need to define childSchema before you reference it in masterSchema, so switch those around to be:

var childSchema = mongoose.Schema({
  number:{type:Number},
  name: {type:String,required:true}

});

childSchema.pre('save',function(next){
  this.number = 1;
  //set number with the total number of childs for the parent master
  console.log('child pre save called!');
  console.log(this);
  next();
});

var masterSchema = mongoose.Schema({
  title:{type:String,required:true},
  childs: [childSchema]
});

masterSchema.pre('save',function(next){
  console.log("master pre save called!");
  console.log(this);
  next();
});
like image 63
JohnnyHK Avatar answered Sep 04 '26 15:09

JohnnyHK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!