Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 531
mkoistinen Avatar asked Nov 29 '22 02:11

mkoistinen


1 Answers

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

like image 195
mpobrien Avatar answered Dec 06 '22 12:12

mpobrien