I'm creating a daycare management web application in NodeJS using ExpressJS mongoose and EJS. Where there is a module to add participants and another module to keep trace of the participants' daily four times meal count. I'm referencing Participant Schema to Attendance Schema. Participant collection shows well on the attendance page but when I send a post request to Attendance Schema it's not passing to the database. As I'm new, I can't find my mistake. Can anyone please help me where I made mistake?
Schema:
const mongoose = require('mongoose')
const attendanceSchema = new mongoose.Schema({
_id: {
type: String,
required: [true, 'Id is required']
},
mDate: {
type: Date,
unique: true,
default: Date.now,
required: [true, 'Date is required'],
aMeal: {
Breakfast: {
type: String,
default: ''
},
Lunch: {
type: String,
default: ''
},
pmSnacks: {
type: String,
default: ''
},
Dinner: {
type: String,
default: ''
}
}
},
participant: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Participant'
}
}, { timestamps: true })
module.exports = mongoose.model('Attendance', attendanceSchema)
Route
const express = require('express')
const router = express.Router()
const Participant = require('../models/participants/new')
const Attendance = require('../models/attendance/new')
router.get('/', async (req, res) => {
showattendance(res, new Attendance())
})
router.post('/', async (req, res) => {
const attendance = new Attendance({
pId: req.body.pId,
Breakfast: req.body.Breakfast,
Lunch: req.body.Lunch,
PmSnacks: req.body.PmSnacks,
Dinner: req.body.Dinner
})
try {
const newAttendance = await attendance.save()
res.redirect(`attendance`)
} catch {
showattendance(res, attendance, true)
}
})
async function showattendance (res, attendance, hasError = false){
try {
const participants = await Participant.find({}).sort({ createdAt: 'desc' })
const params = {
participants: participants,
attendance: attendance
}
if (hasError) params.Msg = 'Error updating attendance'
res.render('attendance', params)
} catch {
res.redirect('/')
}
}
module.exports = router
View
<% if (participants.lenth !== 0) { participants.forEach(participant => { %>
<div class="flex bg-light">
<div class="flex w-55 bt-white-1">
<div class="w-20 p-5-10 br-white-1"><a href="/participant/edit"><%= participant._id %></a></div>
<div class="w-35 p-5-10 br-white-1"><%= participant.fName %> <%= participant.lName %></div>
<div class="w-15 p-5-10 br-white-1"><%= participant.Schedule %></div>
<div class="w-30 p-5-10 br-white-1"><%= participant.Grade %></div>
</div>
<input type="hidden" name="pId" value="<%= participant._id %>">
<div class="flex w-45 bt-white-1 text-center">
<div class="w-25 p-5-10 br-white-1">
<label for="Breakfast">
<% if (participant._id === attendance.participant && attendance.mDate.aMeal.Breakfast === 'Yes' ) { %>
<input type="checkbox" checked value="Yes" onChange=countChecked() name="Breakfast">
<% } else { %>
<input type="checkbox" value="Yes" onChange=countChecked() name="Breakfast">
<% } %>
</label>
</div>
<div class="w-25 p-5-10 br-white-1">
...
</div>
</div>
</div>
<% })} else { %>
<div class="bg-theme">
<h1 class="text-white text-center m-0">No record found!</h1>
</div>
<% } %>