mkoistinen
mkoistinen

Reputation: 7773

Access the list of valid values for an Enum field in a Mongoose.js Schema

The other day I saw a comment on the Web somewhere telling one how to access the list of values defined for an Enum field in a Mongoose.js Schema. Sadly, I didn't commit that tidbit or its URL to memory and now I need it!

Does anyone know how to do this?

Thanks in advance!

Upvotes: 4

Views: 11397

Answers (2)

banana
banana

Reputation: 369

In case anyone stumbles upon this like I did, what worked for me in mongoose 5.x was:

Temp.schema.path('salutation').options.enums;

Upvotes: 0

mpobrien
mpobrien

Reputation: 4962

Is this what you're looking for?

var mongoose = require('./index')
, TempSchema = new mongoose.Schema({
salutation: {type: String, enum: ['Mr.', 'Mrs.', 'Ms.']}
});

var Temp = mongoose.model('Temp', TempSchema);

console.log(Temp.schema.path('salutation').enumValues);
var temp = new Temp();
console.log(temp.schema.path('salutation').enumValues);

Source: https://gist.github.com/953059

Upvotes: 22

Related Questions