-
-
Save bnoguchi/953059 to your computer and use it in GitHub Desktop.
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); |
thanks!!! :)
great!
Thanks
So neat, but doesn't work for me (mongoose 3.6.20): for the path() function, my enum fields must be prefixed with ".enum", and there is no "enumValues" property on the returned object. This works for me... console.log(Temp.schema.path('salutation.enum').options.type) // We get ['Mr.', 'Mrs.', 'Ms.']
Update: bnoguchi's neater version works if you do Temp.path("salutation").enum('Mr.', 'Mrs.', 'Ms.');
after you've defined the schema (i was using salutation: {enum: ['Mr.', 'Mrs.', 'Ms.']}
in the Schema constructor parameter, which doesn't seem to work as well).
I've salutation sub-document inside profile document, so Temp.schema.path('profile.salutation').enumValues
works in my case.
Thanks!
Great!!
This was very helpful. Thanks
Thanks a lot, helpful!!
Thank you
If you want to know all the enum values inside in array type, you need something like this:
temp.path('salutation.0.type').enumValues
👍 This kind of thing is possible too :
collaboration.schema.path('typeRef').enumValues[0]
Wow, I was just wondering how to do this as I need the enum data to implement a droplist. Very helpful, thanks!
Thanks
Perfect, thank you!
Thank you!
How to access the enum from a non-model schema? Like if I am using a discriminator schema and in that schema, there is enum field, how can I can access that?
nice!
nice
thanks.
To find enums of all enum properties:
const schema = Temp.schema;
const enums = Object.keys(schema.obj)
.filter(k => schema.obj[k].hasOwnProperty('enum'))
.map(k => {
let o = {};
o[k] = schema.obj[k].enum;
return o;
})
.reduce((r, v) => Object.assign(r, v), {});
Just what I needed. Thank you!
for me it worked like this:
//model typeContacts.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const arrayTypeContacts = [
{ type: 'phone', name: 'principal', icon: 'phone' },
{ type: 'email', name: 'principal', icon: 'envelope' },
{ type: 'whatsapp', name: 'principal', icon: 'whatsapp' },
];
const TypeConctactSchema = Schema({
contactType: {
type: String,
enum: arrayTypeContacts,
default: arrayTypeContacts
}
});
const TypeContacts = mongoose.model('typecontacts', TypeConctactSchema);
module.exports = TypeContacts;
//controller
const data = await TypeContactModel.schema.path('contactType').options.enum;
thank you a lot!!
thanks!