猫鼬-如何将架构中间件利用到'init'事件中? [英] Mongoose - how to tap schema middleware into the 'init' event?

查看:55
本文介绍了猫鼬-如何将架构中间件利用到'init'事件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

猫鼬文档建议,我应该能够使用插入到"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.

这篇关于猫鼬-如何将架构中间件利用到'init'事件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆