Categories
JavaScript Answers

How to Create and Use Enum in Mongoose and JavaScript?

Spread the love

To create and use enum in Mongoose and JavaScript, we can add an enum field into the schema.

For instance, we write

const userSchema = new mongoose.Schema({
  userType: {
    type: String,
    enum: ["user", "admin"],
    default: "user",
  },
});

to create a schema with Schema.

We call it with an object that has the enum property with the possible values for the enum to create the userType enum field that is a string that can either be 'user' or 'admin'.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *