简体   繁体   中英

Unknown error when using bcrypt.hash

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM