Skip to main content

User Model

The user model is a Mongoose model that defines the structure of the user data in the database. The user model is used to interact with the user data in the database.

Schema

export const userModel = mongoose.Schema({
email: String,
birthdate: Date,
first_name: String,
middle_name_initial: String,
last_name_initial: String,
county_name: String,
join_date: Date,
});

export const User = mongoose.models.users || mongoose.model("users", userModel);

Example Usage

import { User } from "../models/user";

export default async function updateUser(userId, data) {
try {
const user = await User.findById(userId);
if (!user) {
throw new Error("User not found");
}
user.email = data.email;
user.birthdate = data.birthdate;
...
await user.save();
return user;
} catch (error) {
throw new Error(error.message);
}
}