我正在使用ExpressJS mongoose和EJS在NodeJS中创建一个日托管理web应用程序。其中有一个模块用于添加参与者,另一个模块用于跟踪参与者每天四次用餐的次数。我正在将Participant Schema引用到考勤模式。参与者集合在出勤页面上显示得很好,但是当我向出勤方案发送post请求时,它不会传递到数据库。由于我是新来的,我找不到我的错误。谁能帮帮我,我哪里弄错了?
模式:
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)路由
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视图
<% 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>
<% } %>发布于 2021-11-03 16:46:23
添加
app.use(express.json()) 和
app.use(express.urlencoded({extended:false})); 定义express的位置
https://stackoverflow.com/questions/69828610
复制相似问题