Home > Software engineering >  Can someone tell me the basic way to make a model.js file under mongo db
Can someone tell me the basic way to make a model.js file under mongo db

Time:11-19

For building a MERN App

const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema({
    name:{
        type: String,
        required: true
    },
    email:{
        type: String,
        required: true,
        unique: true
    },
    avatar: {
        type: String
    },
    password: {
        type: String,
        required: true
      },
    date: {
         type: Date,
         default: Date.now
    }
});

module.exports = User = mongoose.model('user',UserSchema);

Expecting to form a user model which can be used to store posts an other things later

CodePudding user response:

Your answer looks like correct to me, you just need to use it in the routes or in any other models as per you need using :

const User = require('../../models/User');

  • Related