I have this pre mongoose middleware for saving passwords,I previously used synchronous implementation,now I am doing an asynchronous implemntation as mongoose middleware:
schema.pre('save', function(next) {
var user = this;
var SALT_WORK_FACTOR = 5;
if (!user.isModified('local.password')) return next();
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
bcrypt.hash(user.local.password, salt, function(err, hash) {
if (err) return next(err);
user.local.password = hash;
next();
});
});
});
The code just throws an unknown error when it gets to bcrypt.hash although the error is previously null. If I use something like stackup,the error looks like this:
E:\Do\login\node_modules\stackup\index.js:32
error.stack = activeTrace.toString(error.stack);
^
TypeError: Cannot assign to read only property 'stack' of No callback function w
as given.
at AsyncListener.error (E:\Do\login\node_modules\stackup\index.js:32:19)
at asyncCatcher (E:\Do\login\node_modules\stackup\node_modules\async-listene
r\glue.js:123:26)
at process._asyncFatalException [as _fatalException] (E:\Do\login\node_modul
es\stackup\node_modules\async-listener\glue.js:211:14)