mongoose, unexpected behavior

38 views
Skip to first unread message

Алексей Фортуна

unread,
Oct 2, 2013, 2:54:45 AM10/2/13
to mongoo...@googlegroups.com

Unexpected behavior

I have some model:

//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);


Do save:

var o = new require('test')({ objectsArray: [{ name: 'Alex' }] });
o
.save(function(err){ console.log('saved'); });



After, in console i get:

pre validate schema1
pre validate schema1
saved

The question is, Why? schema1.pre('validate')- appears 2 times, but i expect 1 time?

Aaron Heckmann

unread,
Oct 2, 2013, 10:37:50 AM10/2/13
to mongoo...@googlegroups.com
add the pre hook to the child schema before passing it to the parent.

schema1.pre(..)
schema2 = Schema({ ary: schema1 })
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--

Алексей Фортуна

unread,
Oct 4, 2013, 5:36:26 AM10/4/13
to mongoo...@googlegroups.com
I did exactly as you told me but the results remained the same. I am forwarding you the complete code:

var mongoose = require('mongoose');
mongoose
.connect('mongodb://127.0.0.1:27017/test');

mongoose
.connection.on('open', function() {


   
var schema1 = new mongoose.Schema({
        name
: String
   
})


    schema1
.pre('validate', function(next) {
        console
.log('pre validate');
       
next();

   
});

   
var schema2 = new mongoose.Schema({
        objectsArray
: [schema1]
   
});


   
Schema2 = mongoose.model('Schema2', schema2);

   
var o = Schema2({ objectsArray: [{ name: 'Alex' }] });

    o
.save(function(err){ console.log('saved'); });

});





среда, 2 октября 2013 г., 17:37:50 UTC+3 пользователь Aaron Heckmann написал:
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages