为什么不是所有值都存储在mongodb中?

问题描述 投票:0回答:1

我想使用MEAN APPLICATION将表单数据存储在“ moongodb”中>

当我通过Angular表单或使用邮递员发布form的值时,所有值都不存储,甚至没有为其创建任何空间。

这里是课程模型

const mongoose = require('mongoose');
const CourseSchema = mongoose.Schema({
        degprog:String,
        session:String,
        semester:String,
        c_code:String,
        c_title:String,
        c_hours:String,
        c_goals:String,
        m_quiz:Number,
        m_assign:Number,
        m_lab:Number,
        m_mid:Number,
        m_final:Number,
        m_total:Number,
        c_coprdinator:String,
        c_url:String,
        c_catelog:String,
        c_tbook:String,
        c_reference:String,

        teacher:{
            type:mongoose.Schema.Types.ObjectId,
            ref:"teacher",

        }
});
module.exports = mongoose.model('course', CourseSchema);

这是课程控制器文件

const Teacher = require('../models/teacher.model.js');
const mongoose = require('mongoose');
// Create and Save a new Course


exports.create = async (req, res) => {
    if (!req.body.courseCode) {
         return res.status(400).send({
            message: "Note Course Code can not be empty"
        });
    }
    //searching for a teacher to add course

    const teacher = await Teacher.findOne({ _id: req.params.teacherId });
    // Create a Course
    const course = new Course();
    course._id = mongoose.Types.ObjectId(),
     course.degprog = req.body. degreeProgram;
     course.session = req.body.session;
    course.semester = req.body.semester;
     course.c_code = req.body.courseCode;
    course.c_title = req.body.courseTitle;
    course.c_hours = req.body.creditHours;
    course.m_quiz = req.body.quiz;
    course.m_assign = req.body.assignment;
    course.c_coprdinator = req.body.courseCoordinator;
    course.c_url = req.body.url;
    course.c_catelog = req.body.courseCatelog;
    course.c_tbook = req.body.textbook;
    course.c_reference = req.body.reference;
    course.c_goals = req.body.goals;
    course.teacher = req.body.teacherId;
    course.m_lab = req.body.lab;
    course.m_mid = req.body.mid;
    course.m_final = req.body.final;
     course.m_total = req.body.total;

    //save course in dataBase and attach to particular teacher 
    await course.save();
    await teacher.courses.push(course._id);
    await teacher.save();
    res.send(course);

};

此是路由文件

app.post('/teacher/:teacherId/course',  course.create);

仅直到“ URL”值存储。为什么剩余的值不保存?

我想使用MEAN APPLICATION将表单数据存储在“ moongodb”中,当我通过Angular表单或使用邮递员发布表单的值时,所有值都不会存储,甚至不会为其创建任何空间。在这里...

node.js mongodb angular7 angular-reactive-forms
1个回答
0
投票

[我觉得这可能是您使用.save()的方式,这是一个使用猫鼬createfindByIdAndUpdate的示例,我还使用对象解构赋值从req.body中提取值

© www.soinside.com 2019 - 2024. All rights reserved.