gpt4 book ai didi

node.js - Mongoose 嵌入式文档主细节回调序列

转载 作者:可可西里 更新时间:2023-11-01 10:44:36 27 4
gpt4 key购买 nike

我尝试在子项的预保存 Hook 中执行一些操作,但它似乎从未被触发。我只解雇了大师的预保存 Hook 。

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

我在 CoffeeScript 中的规范:

 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)

我错过了什么吗?根据我的理解,每当我将 child 添加到集合中并在事后保存时, child 应该预先保存火。我做错了什么?我可以将 child 添加到大师收藏中并根据大师值预设值吗?

感谢您的帮助...

最佳答案

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

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

关于node.js - Mongoose 嵌入式文档主细节回调序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12037307/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com