ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ### set() 新增数据时触发 ~~~ 'use strict'; const moment = require('moment') const bcrypt = require('bcryptjs') module.exports = app => { const { STRING, INTEGER, DATE } = app.Sequelize; const User = app.model.define('user', { id: { type: INTEGER(20).UNSIGNED, primaryKey: true, autoIncrement: true }, nickname: { type: STRING}, password: { type: STRING, // 密码自动加盐 set(val) { const salt = bcrypt.genSaltSync(10) const pwd = bcrypt.hashSync(val, salt) // 传入2个参数,第一个是字段名称,第二个是加密字符串 this.setDataValue('password',pwd) } }, email: { type: STRING}, openid: { type: STRING} }); return User; }; ~~~