1024programmer Java Access static methods in Mongoose instance methods

Access static methods in Mongoose instance methods

The method is relatively simple, rough and ugly, which is to access the static method through the constructor, roughly as follows:

1
2
3
4
5
6
7
8
9
WorkSpaceSchema.methods.getPrice = function(startTime, endTime){
// ...
var result = days * Math.floor(this.constructor.getPricePerDay(this.discountPrice || this.price, this.priceType));
// ...
};

WorkSpaceSchema.statics.getPricePerDay = function(price, priceType){
// code
};

After seeing this solution, my first thought was why Mongoose didn’t consider the access between methods when designing it. If you think about it carefully, the original intention of the static method is just to pass the Model already. There are ways to extend and customize it, create some special search methods, etc., and if you want to create methods that are called in other methods, a better solution is to write a separate module and then export and call it.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/770718

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索