2

我尝试在子项的预保存挂钩中进行一些操作,但它似乎从未被解雇。我只得到了主人的预保存钩子。

我的 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)];

我在咖啡脚本中的规格:

 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()

这是我的规范结果,永远不会调用 childs pre-save:

 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)

我错过了什么吗?据我了解,每当我将孩子添加到集合中并在之后保存时,孩子是否应该预先救火。我做错了什么?我可以将一个孩子添加到一个主集合中并根据主值预设值吗?

谢谢你的帮助...

4

1 回答 1

1

您需要childSchema在引用它之前定义它masterSchema,因此将它们切换为:

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();
});
于 2012-08-20T15:17:09.230 回答