I'm using Mongoose with promises by wrapping it this way with bluebird's method:
var mongoose = promise.promisifyAll(require('mongoose'))
Then I'm trying to use an async method:
var newUser = new User({email: 'd@a.com', password: '123', userType: 'admin', name: 'paco'});
newUser.saveAsync()
.then(function (createdUser) {
console.log(createdUser);
should.exist(createdUser);
done();
})
.catch(function (err) {
console.log(err);
should.not.exist(err);
done();
});
However, as the schema got an unique index for email field, if I try to run this multiple times an Unhandled rejection AssertionError: expected Error
will be thrown causing it all to fail.
How can I properly handle any error?