neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

pre hook with next(new Error("x")) saves anyway! #585

Closed janek26 closed 7 years ago

janek26 commented 8 years ago

My code is like:

var errors = thinky.Errors;

User.pre('save', function(next) {
  var passwordStrength = 2;
  if (passwordStrength.score >=3 ){
    next();
  } else {
    next(new errors.ValidationError("password"));
  };
});

In this case I thought thinky is just saving when next() is called without arguments, or do I miss something? Thinky does save anyway, even if the password in this case isn't strong enough!

neumino commented 7 years ago

Works for me:

var thinky = require('./lib/thinky.js')();
var type = thinky.type;

var Model = thinky.createModel('Foo', {
  id: type.string(),
  age: type.number()
});

Model.pre('save', function(next) {
  if (this.age > 18) {
    next();
  } else {
    next(new Error('FOO'));
  }
});

var a = new Model({
  id: 'bar',
  age: 2
});

a.save().then(function() {
  console.log('Success save');
}).error(function() {
  console.log('Failure save');
}).finally(function() {
  return Model.get('bar').run();
}).then(function() {
  console.log('Success retrieve');
}).error(function() {
  console.log('Failure retrieve');
});
$ node test.js
Creating a pool connected to localhost:28015
Failure save
Failure retrieve

Please provide a script if that's still an issue.