I have created a nodejs backend project with mongodb as the database. I want to create a document where it stores details of multiple students. Below shows my controller and model files.
Controller.js
const _user = new User({
username: req.body.username,
role: req.body.role,
hash_password: hash_password,
re_hash_password: req.body.re_hash_password,
student:{
member1: {
fullName: req.body.student.member1.fullName,
sliit_id: req.body.student.member1.sliit_id,
phone: req.body.student.member1.phone,
email: req.body.student.member1.email,
specialization: req.body.student.member1.specialization,
},
member2: {
fullName: req.body.student.member2.fullName,
sliit_id: req.body.student.member2.sliit_id,
phone: req.body.student.member2.phone,
email: req.body.student.member2..email,
specialization: req.body.student.member2.specialization,
},
}
Above shows the controller of my nodejs project. Here I want to add multiple members to my mongodb database with student object.
Model.js
const demoStudentSchema = new mongoose.Schema({
fullName: {
type: String,
required: true,
trim: true,
min: 3,
max: 30
},
sliit_id: {
type: String,
required: true,
trim: true,
min: 3,
max: 20
},
phone: {
type: String,
required: true
},
email: {
type: String,
required: true,
trim: true,
unique: true,
lowercase: true
},
specialization: {
type: String,
enum: ['SE','CSNE','DS', 'ISE', 'CS']
},
})
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
trim: true,
unique: true,
index: true,
min: 3,
max: 20
},
role: {
type: String,
required:true,
enum: ['student','supervisor','staff']
},
hash_password: {
type: String,
required: true,
},
re_hash_password: {
type: String,
required: true,
},
student: {
member1: {
demoStudentSchema
},
member2: {
demoStudentSchema
}
}
When I try to run this code using postman, below error occurs.
{
"message": {
"errors": {
"email": {
"name": "ValidatorError",
"message": "Path `email` is required.",
"properties": {
"message": "Path `email` is required.",
"type": "required",
"path": "email"
},
"kind": "required",
"path": "email"
},
"phone": {
"name": "ValidatorError",
"message": "Path `phone` is required.",
"properties": {
"message": "Path `phone` is required.",
"type": "required",
"path": "phone"
},
"kind": "required",
"path": "phone"
},
"sliit_id": {
"name": "ValidatorError",
"message": "Path `sliit_id` is required.",
"properties": {
"message": "Path `sliit_id` is required.",
"type": "required",
"path": "sliit_id"
},
"kind": "required",
"path": "sliit_id"
},
"fullName": {
"name": "ValidatorError",
"message": "Path `fullName` is required.",
"properties": {
"message": "Path `fullName` is required.",
"type": "required",
"path": "fullName"
},
"kind": "required",
"path": "fullName"
}
},
"_message": "User validation failed",
"name": "ValidationError",
"message": "User validation failed: email: Path `email` is required., phone: Path `phone` is required., sliit_id: Path `sliit_id` is required., fullName: Path `fullName` is required."
}
}
I have gone through some videos and articles but couldnt find out any solution. Could you please help me to find out the error and give your suggestions on solving this issue.