I am working through a tutorial, building a small library DB in Express and Mongoose. In the tutorial each book can have one of several statuses, and only one of those values. It's an Enum. I am handling this by putting an array of values in the BookInstance schema:
var the_statuses = ['Available', 'Maintenance', 'Loaned', 'Reserved'];
which works fine when I have an instance -- I use a virtual method to fetch those values
BookInstanceSchema
.virtual('statuses')
.get(function() {
return the_statuses;
});
when I, as said, have an instance already.
And when creating the Schema I access my local variable as, well, a local variable:
var BookInstanceSchema = Schema({
book: { type: Schema.ObjectId, ref: 'Book', required: true }, //reference to the associated book
imprint: {type: String, required: true},
status: {type: String, required: true, enum: the_statuses, default: 'Maintenance'},
due_back: {type: Date, default: Date.now},
});
But what if I want to simply get the enum values directly from the schema but from outside the schema? That is, if I want to create, in this case, a BookInstance instance, and offer the user a choice of statuses in a select menu, I won't have access to an instance yet (unless I kludge it and simply create one to grab the enum values).
I tried making a static function
BookInstanceSchema.statics.getStatuses = function() {
return this.the_statuses;
};
But it doesn't seem to work.
What's the best way to do what I'm trying to do? Is there such a thing as a "static virtual method" in Mongoose?