简体   繁体   English

在 ZCCADCDEDB567ABAE643E15DCF0974E503Z 中,我将使用 .pre() 中间件 function 做什么?

[英]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.

相关问题 如何在 mongoose 中重用 pre 中间件功能 - How to reuse a pre middleware function in mongoose 在预中间件中未定义的猫鼬模型 - Mongoose Model Undefined in Pre Middleware mongoose中使用pre中间件删除相关记录 - Delete related records using pre middleware in mongoose Mongoose - 如何在预(保存/更新)中间件中抛出多个错误? - Mongoose - How can I throw more than one error in pre (save/update) middleware? 为什么我会使用Redux Promise Middleware而不是Redux Promise? - Why would I use Redux Promise Middleware over Redux Promise? 为什么不调用用{query:true}删除的猫鼬中间件“ pre”? - Why mongoose middleware “pre” remove with { query: true } is not called? Mongoose 前/后中间件没有被调用 model.save - Mongoose pre/post middleware not getting called for model.save Mongoose预保存第二个保存操作未调用的子文档中间件 - Mongoose pre save middleware of subdocument not called on second save operation 在保存前和保存后的 Mongoose 中间件方法之间共享数据 - Sharing data between Mongoose middleware methods pre save and post save Express JS Middleware Mongoose 过滤器和排序查询我没有得到我期望得到的结果,它没有排序 - Express JS Middleware Mongoose filter and sort query i dont get what i expect to get and it's not sorting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM