//对密码进行加密 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() }) }) })
QuizSchema.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); } });
// Experimental // Delete all user related accounts, cards, messages and transfers when he is removed // It probably won't happen in a real world banking app userSchema.pre('remove', async function(next) { const user = this; await Account.deleteMany({ owner: user._id }); await Card.deleteMany({ owner: user._id }); await Message.deleteMany({ sender: user._id, recipient: user._id }); await Transfer.deleteMany({ sender: user._id, recipient: user._id }); next(); });
QuestionSchema.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() })
CategorySchema.pre('save', function(next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); } else{ this.meta.updateAt = Date.now(); } next(); });
MusicSchema.pre('save', function(next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now(); } else{ this.meta.updateAt = 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(); });