意外行为
我有一些模型:
//test.js
var mongoose = require('../utils/mongoose');
var schema1 = new mongoose.Schema({
name: String
})
var schema2 = new mongoose.Schema({
objectsArray: [schema1]
});
schema1.pre('validate', function(next) {
console.log('pre validate schema1');
next();
});
module.exports = mongoose.model('Schema2', schema2);
保存:
var o = new require('test')({ objectsArray: [{ name: 'Alex' }] });
o.save(function(err){
console.log('saved');
});
之后,在控制台中我得到:
pre validate schema1
pre validate schema1
saved
问题是,为什么?
schema1.pre('validate')
- 出现 2 次,但我预计 1 次?