如何从猫鼬的实例方法访问静态方法? [英] How to access a static method from a instance method in mongoose?

查看:55
本文介绍了如何从猫鼬的实例方法访问静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从猫鼬的实例方法访问静态方法?

How to access a static method from a instance method in mongoose?

我有一个名为Job的工作模型.我想从实例方法job.start调用静态方法Job.someStatic().如何从实例方法中的"this"获取作业的引用?

I have a job model named Job. From a instance method job.start I want to call the static method Job.someStatic(). How do I get the reference to the Job, from the "this" in the instance method?

谢谢

推荐答案

我发现一般这样做的唯一方法(而不仅仅是调用Job.someStatic())是:

The only way I've found to do that generically (without just calling Job.someStatic()) is:

this.model(this.constructor.modelName).someStatic();

更新,感谢@ numbers1311407:

Update thanks to @numbers1311407:

我不知道是否总是这样,但是至少从Mongoose 3.6.11开始,您可以将其缩短为:

I don't know if it's always been the case, but as of at least Mongoose 3.6.11, you can shorten this to:

this.constructor.someStatic();

猫鼬4.x更新

这仍然适用于4.4.12.

This still works in 4.4.12.

这篇关于如何从猫鼬的实例方法访问静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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