简体   繁体   中英

In Mongoose, what would I use .pre() middleware function for?

I am just wondering what mongoose.pre('validate') and.pre('save') would be used for. I know how it works and what it does, but I am not sure what situation I could have that I would use it for. Couldn't I just do everything I need within code? And what is difference between validate and save?

Sorry if these are dumb questions, I've done my research but not sure when to use these features

We use the.pre() function that is executed before or after a certain action that you specify. When you want to run a function every time before you save a document in the DB, we would you a pre-hook for that.

Example: Before saving a user to the database, you could call a function that would log the user into to a logging service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM