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

不问归期 提交于 2019-12-03 16:27:24

问题


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

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?

thanks


回答1:


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

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

Update thanks to @numbers1311407:

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();

Mongoose 4.x Update

This still works in 4.4.12.




回答2:


Another option to access statics is:

this.schema.statics.someStatic()


来源:https://stackoverflow.com/questions/14277518/how-to-access-a-static-method-from-a-instance-method-in-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!