0

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)
0

1 Answer 1

1

You're using bcrypt-nodejs, which expects two callbacks:

hash(data, salt, progress, cb)

docs

You've only provided it once, and so cb isn't defined when bcrypt-nodejs hits it.

Sign up to request clarification or add additional context in comments.

1 Comment

The progress callback is optional, per the docs. If you pass hash() three arguments instead of four, it assumes you have omitted the progress callback. (I just tested it myself to confirm.) So I don't think that's the problem here. (It's what I immediately thought too, when I read the docs, though.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.