2
const userModel = connection.define('user', {
user_id: {
  type: Sequelize.BIGINT,
  primaryKey: true,
  autoIncrement: true
},
user_name: {
  type: Sequelize.STRING,
  unique: true
},
email: {
  type: Sequelize.STRING,
  unique: true
},
password: {
  type: Sequelize.STRING
}
},
{
hooks: {
  beforeCreate: () =>{
    console.log("hook called");
    return new Promise((resolve,reject) => {
      const salt = bcrypt.genSalt(10);
      const passwordHash = bcrypt.hash(userModel.password, salt);
      userModel.password = passwordHash;
      console.log('simple pass', userModel.password);
      console.log('hashed password', passwordHash);
      return resolve(userModel);
    });


  }
}
});

It returns this error. hook called simple pass Promise { } hashed password Promise { } Executing (default): INSERT INTO "users" ("user_id","user_name","email","password","createdAt","updatedAt") VALUES (DEFAULT,'deaehrddjelltariq','[email protected]','abcd','2018-02-15 00:17:51.272 +00:00','2018-02-15 00:17:51.272 +00:00') RETURNING *; (node:13686) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, object at _async (/Users/mfaisal/NodeProjects/chaffer/node_modules/bcryptjs/dist/bcrypt.js:214:46) at /Users/mfaisal/NodeProjects/chaffer/node_modules/bcryptjs/dist/bcrypt.js:223:17 at new Promise () at Object.bcrypt.hash (/Users/mfaisal/NodeProjects/chaffer/node_modules/bcryptjs/dist/bcrypt.js:222:20) at Promise (/Users/mfaisal/NodeProjects/chaffer/models/user.js:29:37) at new Promise () at Function.beforeCreate (/Users/mfaisal/NodeProjects/chaffer/models/user.js:27:14) at Promise.each.hook (/Users/mfaisal/NodeProjects/chaffer/node_modules/sequelize/lib/hooks.js:130:35) at tryCatcher (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/util.js:16:23) at Object.gotValue (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/reduce.js:155:18) at Object.gotAccum (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/reduce.js:144:25) at Object.tryCatcher (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromiseCtx (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/promise.js:606:10) at Async._drainQueue (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/async.js:138:12) at Async._drainQueues (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:756:18) at tryOnImmediate (timers.js:717:5) at processImmediate [as _immediateCallback] (timers.js:697:5) (node:13686) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:13686) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1
  • please see that I get "simple pass Promise { }" for "console.log('simple pass, userModel.password);" and similar of passwordHash. Commented Feb 15, 2018 at 0:25

1 Answer 1

2

Replace your hook with this :

With the help of hashSync there is no need of promise

beforeCreate : (user , options) => {
    {
        console.log("hook called");

        //const salt = bcrypt.genSalt(10);
        user.password = user.password && user.password != "" ? bcrypt.hashSync(user.password, 10) : "";

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

1 Comment

Thank you a minor change. I don't why but "bcrypt.hashSync" was throwing illegal argument exception with 'Salt' in the arguments. I replaced it with an integer and now it works fine.

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.