If Mongoose models are designed to work with the instance, and statics are to work with the schema, then why / how does a static method provide access to the document object?
Example:
The following code allows me to convert from a Mongoose document to a JSON document:
appSchema.statics.toItem = function (done) {
var item = toItemSync(this);
return done(null, item);
};
function toItemSync (model) {
return {
id : model.id,
[snip]
};
}
var App = mongoose.model('App', appSchema);
module.exports = App;
I'm calling the toItem()
from my routes to strip off info from the document that should not be sent to the client...
models.Application.findById(id, function(err, app) {
if (err) { return next(err); }
if (!app) { return next('Invalid ID'); }
app.toItem(function(err, appItem){
if (err) { return next(err); }
res.send(appItem);
});
});
All of the docs I'm reading lead me to believe this is not (or should not) be possible:
What is the difference between methods and statics in Mongoose?