【问题标题】:Data not saving to the MongoDB database using NodeJS, ExpressJS and EJS数据未使用 NodeJS、ExpressJS 和 EJS 保存到 MongoDB 数据库
【发布时间】:2021-12-18 01:18:39
【问题描述】:

我正在使用 ExpressJS mongoose 和 EJS 在 NodeJS 中创建一个日托管理 Web 应用程序。有一个模块可以添加参与者,另一个模块可以跟踪参与者每天的四次用餐次数。我将参与者模式引用到出勤模式。参与者集合在出勤页面上显示良好,但是当我向出勤模式发送发布请求时,它并没有传递到数据库。因为我是新人,所以我找不到我的错误。任何人都可以帮我解决我犯的错误吗?

架构:

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>
<% } %>

【问题讨论】:

    标签: node.js mongodb express mongoose ejs


    【解决方案1】:

    添加

    app.use(express.json()) 
    

    app.use(express.urlencoded({extended:false})); 
    

    你定义快递的地方

    【讨论】:

    • 我已经将它们添加到我的 server.js 文件中
    • 现在检查你从前端获取的数据
    • 我之前已经将它们添加到我的 server.js 文件中。我在别的地方搞砸了。
    • 在你的post route中你能得到req.body吗?
    • 是的,我已经登录到控制台i.postimg.cc/kMSY9t5Y/Screenshot-2021-11-04-035003.png
    猜你喜欢
    • 2017-11-19
    • 2021-06-20
    • 2017-05-12
    • 2019-11-03
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多