Mongoose - 如何将架构中间件接入“init"事件? [英] Mongoose - how to tap schema middleware into the 'init' event?
本文介绍了Mongoose - 如何将架构中间件接入“init"事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Mongoose 文档 中建议我应该能够使用插入到init"钩子.
It is suggested in the Mongoose docs that I should be able to control the flow using middleware that plugs in to the "init" hook.
但是,到目前为止,我只在保存"和验证"方面取得了成功.
However, I have so far had success only with "save" and "validate".
当我做这样的事情时,这些中间件都不会被调用:
When I do something like this, neither of these middleware ever get called:
MySchema.post( "init", function (next) { console.log("post init") });
MySchema.pre( "init", function (next) { console.log("pre init") });
我错过了什么吗?
推荐答案
事实证明,init"事件/钩子在创建新模型时不会触发,只有在从数据库加载现有模型时才会触发.看来我应该改用 pre/validate 钩子.
It turns out that the "init" event/hook is not fired when creating a new Model, it is only fired, when loading an existing model from the database. It seems that I should use the pre/validate hook instead.
这篇关于Mongoose - 如何将架构中间件接入“init"事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文