[英]mongoose where to put model functions that can be exposed globally
I am using utils.js
right now which can be required anywhere in my API controllers. 我现在正在使用
utils.js
,这在我的API控制器中的任何地方都可能需要。
I'm wondering if this is a good way of doing it. 我想知道这是否是个好方法。
I might call methods whenever I have a Job object like: 每当我有一个Job对象时,我都可能会调用方法:
var require('../utils');
//express route callback
job = utils.job.getCreatorProfile(job);
res.send(job);
You would typically want to create a method like getCreatorProfile
as a static method of your model by defining it on your schema: 通常,您需要通过在模式上进行定义,来创建诸如
getCreatorProfile
类的方法作为模型的静态方法 :
jobSchema.statics.getCreatorProfile = function(job) { ... };
Which makes it available everywhere as a method on the model class: 这使得它可以作为模型类的一种方法在任何地方使用:
job = Job.getCreatorProfile(job);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.