[英]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.我只是想知道 mongoose.pre('validate') 和.pre('save') 将用于什么。 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.我们使用在您指定的某个操作之前或之后执行的.pre() function。 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.
如果您想在每次将文档保存到数据库之前运行 function,我们会为您提供预挂钩。
Example: Before saving a user to the database, you could call a function that would log the user into to a logging service.示例:在将用户保存到数据库之前,您可以调用 function 将用户登录到日志服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.