How to Create and Use Enum in Mongoose How to Create and Use Enum in Mongoose express express

How to Create and Use Enum in Mongoose


The enums here are basically String objects. Change the enum line to enum: ['NEW', 'STATUS'] instead. You have a typo there with your quotation marks.


From the docs

Mongoose has several inbuilt validators. Strings have enum as one of the validators.So enum creates a validator and checks if the value is given in an array.E.g:

var userSchema = new mongooseSchema({   userType: {        type: String,        enum : ['user','admin'],        default: 'user'    },})


Let say we have a enum Role defined by

export enum Role {  ADMIN = 'ADMIN',  USER = 'USER'}

We can use it as type like:

{    type: String,    enum: Role,    default: Role.USER,}