首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未使用NodeJS、ExpressJS和EJS保存到MongoDB数据库的数据

未使用NodeJS、ExpressJS和EJS保存到MongoDB数据库的数据
EN

Stack Overflow用户
提问于 2021-11-03 16:41:51
回答 1查看 33关注 0票数 0

我正在使用ExpressJS mongoose和EJS在NodeJS中创建一个日托管理web应用程序。其中有一个模块用于添加参与者,另一个模块用于跟踪参与者每天四次用餐的次数。我正在将Participant Schema引用到考勤模式。参与者集合在出勤页面上显示得很好,但是当我向出勤方案发送post请求时,它不会传递到数据库。由于我是新来的,我找不到我的错误。谁能帮帮我,我哪里弄错了?

模式:

代码语言:javascript
复制
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)

路由

代码语言:javascript
复制
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

视图

代码语言:javascript
复制
<% 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>
<% } %>
EN

回答 1

Stack Overflow用户

发布于 2021-11-03 16:46:23

添加

代码语言:javascript
复制
app.use(express.json()) 

代码语言:javascript
复制
app.use(express.urlencoded({extended:false})); 

定义express的位置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69828610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档