Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

grandchild save hook error

  • Loading branch information...
commit dcd05dd67865f376ef1b8057cda989c3a8c4ec87 1 parent bcbf90d
Lenin gitfy authored aheckmann committed
Showing with 73 additions and 82 deletions.
  1. +30 −30 lib/document.js
  2. +0 −38 lib/model.js
  3. +43 −14 test/model.test.js
60 lib/document.js
View
@@ -745,37 +745,37 @@ Document.prototype._registerHooks = function () {
if (!this.save) return;
this.pre('save', function (next) {
- // we keep the error semaphore to make sure we don't
- // call `save` unnecessarily (we only need 1 error)
- var subdocs = 0
- , DocumentArray = require('./types/documentarray')
- , error = false
- , self = this;
-
- var arrays = this._activePaths
- .map('init', 'modify', function (i) {
- return self.getValue(i);
- })
- .filter(function (val) {
- return (val && val instanceof DocumentArray && val.length);
- });
+ // we keep the error semaphore to make sure we don't
+ // call `save` unnecessarily (we only need 1 error)
+ var subdocs = 0
+ , DocumentArray = require('./types/documentarray')
+ , error = false
+ , self = this;
+
+ var arrays = this._activePaths
+ .map('init', 'modify', function (i) {
+ return self.getValue(i);
+ })
+ .filter(function (val) {
+ return (val && val instanceof DocumentArray && val.length);
+ });
- if (!arrays.length)
- return next();
-//require('sys').puts(this.name);
- arrays.forEach(function (array) {
- subdocs += array.length;
- array.forEach(function (value) {
- if (!error)
- value.save(function (err) {
- if (!error) {
- if (err) {
- error = true;
- next(err);
- } else
- --subdocs || next();
- }
- });
+ if (!arrays.length)
+ return next();
+
+ arrays.forEach(function (array) {
+ subdocs += array.length;
+ array.forEach(function (value) {
+ if (!error)
+ value.save(function (err) {
+ if (!error) {
+ if (err) {
+ error = true;
+ next(err);
+ } else
+ --subdocs || next();
+ }
+ });
});
});
}, function (err) {
38 lib/model.js
View
@@ -456,44 +456,6 @@ Model.prototype.remove = function remove (fn) {
*/
Model.prototype._registerHooks = function registerHooks () {
- // make sure to pass along all the errors from subdocuments
-// this.pre('save', function (next) {
-// // we keep the error semaphore to make sure we don't
-// // call `save` unnecessarily (we only need 1 error)
-// var subdocs = 0
-// , error = false
-// , self = this;
-//
-// var arrays = this._activePaths
-// .map('init', 'modify', function (i) {
-// return self.getValue(i);
-// })
-// .filter(function (val) {
-// return (val && val instanceof DocumentArray && val.length);
-// });
-//
-// if (!arrays.length)
-// return next();
-//
-// arrays.forEach(function (array) {
-// subdocs += array.length;
-// array.forEach(function (value) {
-// if (!error)
-// value.save(function (err) {
-// if (!error) {
-// if (err) {
-// error = true;
-// next(err);
-// } else
-// --subdocs || next();
-// }
-// });
-// });
-// });
-// }, function (err) {
-// this.db.emit('error', err);
-// });
-
Document.prototype._registerHooks.call(this);
};
57 test/model.test.js
View
@@ -3145,17 +3145,49 @@ module.exports = {
},
'pre hooks called on all sub levels': function () {
+ var db = start();
+ var grandSchema = new Schema({ name : String });
+ grandSchema.pre('save', function (next) {
+ this.name = 'grand';
+ next();
+ });
+ var childSchema = new Schema({ name : String, grand : [grandSchema]});
+ childSchema.pre('save', function (next) {
+ this.name = 'child';
+ next();
+ });
+ var schema = new Schema({ name: String, child : [childSchema] });
+
+ schema.pre('save', function (next) {
+ this.name = 'parent';
+ next();
+ });
+
+ var S = db.model('presave_hook', schema, 'presave_hook');
+ var s = new S({ name : 'a' , child : [ { name : 'b', grand : [{ name : 'c'}] } ]});
+
+ s.save(function (err, doc) {
+ db.close();
+ should.strictEqual(null, err);
+ doc.name.should.eql('parent');
+ doc.name.child[0].name.should.eql('child');
+ doc.name.child[0].grand[0].name.should.eql('grand');
+
+ });
+
+ },
+
+ 'pre hooks error on all sub levels': function () {
var db = start();
var grandSchema = new Schema({ name : String });
grandSchema.pre('save', function (next) {
- this.name = 'grand';
- next();
- });
+ next(new Error('Error 101'));
+ });
var childSchema = new Schema({ name : String, grand : [grandSchema]});
childSchema.pre('save', function (next) {
- this.name = 'child';
- next();
- });
+ this.name = 'child';
+ next();
+ });
var schema = new Schema({ name: String, child : [childSchema] });
schema.pre('save', function (next) {
@@ -3163,21 +3195,18 @@ module.exports = {
next();
});
- var S = db.model('presave_hook', schema, 'presave_hook');
+ var S = db.model('presave_hook_error', schema, 'presave_hook_error');
var s = new S({ name : 'a' , child : [ { name : 'b', grand : [{ name : 'c'}] } ]});
s.save(function (err, doc) {
db.close();
- should.strictEqual(null, err);
- doc.name.should.eql('parent');
- doc.name.child[0].name.should.eql('child');
- doc.name.child[0].grand[0].name.should.eql('grand');
-
+ err.should.be.an.instanceof(Error);
+ err.message.should.eql('Error 101');
});
},
-
- 'test post hooks': function () {
+
+ 'test post hooks': function () {
var schema = new Schema({
title: String
})
Please sign in to comment.
Something went wrong with that request. Please try again.