I have a piece of code which finds the record with the highest value for a Numeric field, 'ordinal':
Job.find({}).sort({'ordinal': -1}).limit(1).then(maxOrd => {
console.log(`Found MaxOrd: ${maxOrd}`);
});
This works fine. Now I'd like to make this a static method of the Job
schema. As so I tried:
JobSchema.statics.findMaxOrdinal = function(callback) {
Job.find({}, callback).sort({'ordinal': -1}).limit(1);
};
...and:
Job.findMaxOrdinal().then(maxOrd => {
console.log(`Found Max Ord using Promise: ${maxOrd}`);
});
But this isn't working, and crashes with a very unhelpful stack trace.
How do I write my static so that I can use it with a Promise?
Just return mongoose query like this:
JobSchema.statics.findMaxOrdinal = function() {
return Job.find({}).sort({'ordinal': -1}).limit(1);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.