//对密码进行加密 UserSchema.pre('save', function (next) { var user = this if (this.isNew) { this.createAt = this.updateAt = Date.now() } else { this.updateAt = Date.now() } bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) { if (err) return next(err) bcrypt.hash(user.password, salt, function (err, hash) { if (err) return next(err) user.password = hash next() }) }) })
UserSchema.pre('save', function(next) { var user = this;
QuestionSchema.pre('save', function (next) { if (this.isNew) { this.meta.createdAt = this.meta.updatedAt = Date.now() } else { this.meta.updatedAt = Date.now() } next() })
/** * Add your * - pre-save hooks * - validations * - virtuals */ userSchema.pre('save', async function save(next) { try { if (!this.isModified('password')) return next(); const rounds = env === 'test' ? 1 : 10; const hash = await bcrypt.hash(this.password, rounds); this.password = hash; return next(); } catch (error) { return next(error); } });
ReplySchema.pre('find', function () { this.populate({ path: 'userId', select: 'channelName photoUrl', sort: '+createdAt' }) })
// Hash the plain text password before saving userSchema.pre('save', async function(next) { const user = this; if (user.isModified('password')) { user.password = await bcrypt.hash(user.password, 8); } next(); });
userSchema.pre('save', function (next) { const user = this; bcrypt.genSalt(10, (err, salt) => { if (err) return next(err); bcrypt.hash(user.password, salt, null, (err, hash) => { if (err) return next(err); user.password = hash; user.access_token = getToken(user); next(); }); }); });
QuizSchema.pre('save', function (next) { if (this.isNew) { this.meta.createdAt = this.meta.updatedAt = Date.now() } else { this.meta.updatedAt = Date.now() } next() })
CourseSchema.pre('save', function (next) { if (this.isNew) { this.meta.createdAt = this.meta.updatedAt = Date.now() } else { this.meta.updatedAt = Date.now() } next() })
StudentSchema.pre('save', function (next) { if (this.isNew) { this.meta.createdAt = this.meta.updatedAt = Date.now() } else { this.meta.updatedAt = Date.now() } next() })
CitySchema.pre('save', function(next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); } else { this.meta.updateAt = Date.now(); } next(); });
CityProgrammeSchema.pre('save', function(next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); } else { this.meta.updateAt = Date.now(); } next(); });
// 模式保存前执行下面函数,如果当前数据是新创建,则创建时间和更新时间都是当前时间,否则更新时间是当前时间 CommentSchema.pre('save', function(next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); } else{ this.meta.updateAt = Date.now(); } next(); });
MusicCategorySchema.pre('save', function(next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); } else{ this.meta.updateAt = Date.now(); } next(); });
CategorySchema.pre('save', function(next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); } else{ this.meta.updateAt = Date.now(); } next(); });