User model

We will implement the user model in the server/models/user.model.js file, using Mongoose to define the schema with the necessary user data fields, to add built-in validation for the fields and to incorporate business logic such as password encryption, authentication, and custom validation.

We will begin by importing the mongoose module and use it to generate a UserSchema.

mern-skeleton/server/models/user.model.js:

import mongoose from 'mongoose'const UserSchema = new mongoose.Schema({ … })

The mongoose.Schema() function takes a schema definition object as a parameter to generate a new Mongoose schema object that can be used in the rest of the backend code.

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.