BeforeCreate挂钩使用bcryptjs

时间:2018-02-15 00:24:27

标签: node.js hook sequelize.js bcrypt

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);
    });


  }
}
});

它返回此错误。 钩叫 简单传递Promise {} 哈希密码Promise {} 执行(默认):INSERT INTO“users”(“user_id”,“user_name”,“email”,“password”,“createdAt”,“updatedAt”)VALUES(DEFAULT,'deaehrddjelltariq','ddehreejd @gmail.com' ,'abcd','2018-02-15 00:17:51.272 +00:00','2018-02-15 00:17:51.272 +00:00')退货*; (node:13686)UnhandledPromiseRejectionWarning:错误:非法参数: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     在新的承诺()     在Object.bcrypt.hash(/Users/mfaisal/NodeProjects/chaffer/node_modules/bcryptjs/dist/bcrypt.js:222:20)     在Promise(/Users/mfaisal/NodeProjects/chaffer/models/user.js:29:37)     在新的承诺()     在Function.beforeCreate(/Users/mfaisal/NodeProjects/chaffer/models/user.js:27:14)     在Promise.each.hook(/Users/mfaisal/NodeProjects/chaffer/node_modules/sequelize/lib/hooks.js:130:35)     在tryCatcher(/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/util.js:16:23)     在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)     在Promise._settlePromiseFromHandler(/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/promise.js:512:31)     在Promise._settlePromise(/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/promise.js:569:18)     在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)     在Immediate.Async.drainQueues(/Users/mfaisal/NodeProjects/chaffer/node_modules/bluebird/js/release/async.js:17:14)     在runCallback(timers.js:756:18)     在tryOnImmediate(timers.js:717:5)     at processImmediate [as _immediateCallback](timers.js:697:5) (node:13686)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误源于在没有catch块的情况下抛出异步函数,或者拒绝未使用.catch()处理的promise。 (拒绝ID:1) (节点:13686)[DEP0018]弃用警告:不推荐使用未处理的拒绝承诺。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。

1 个答案:

答案 0 :(得分:2)

将此Excel::create("Ingresos Agentes", function($excel) use ($salida) { $excel->sheet("Hoja 1", function($sheet) use ($salida) { foreach ($salida as $key => $sali) { $sheet->row($key+2, ['Col 1', 'Col 2', 'Col 3', 'Col 4', 'Col 5', 'Col 6', 'Col 7', 'Col 8', 'Col 9', 'Col 10', 'Col 11', 'Col 12', 'Col 13', 'Col 14', 'Col 15', 'Col 16', 'Col 17']); switch ($sali['Letra']) { case 'A': $sheet->row($key+2, function($row) { $row->setBackground('#00C851'); }); break; case 'B': $sheet->row($key+2, function($row) { $row->setBackground('#FFBB33'); }); break; case 'C': $sheet->row($key+2, function($row) { $row->setBackground('#33B5E5'); }); break; case 'Z': $sheet->row($key+2, function($row) { $row->setBackground('#FF4444'); }); break; } } $sheet->fromModel(@$salida, null, "", false, true); }); })->download('xls'); 替换为:

hook的帮助下,无需hashSync

promise