Node.js 无法从学生模型记录类模型数据 [英] Node.js cant log Class model data from Student model

查看:42
本文介绍了Node.js 无法从学生模型记录类模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了问题.我目前正在使用 node.js 和 MongoDB 创建一个学校目录.我在 app.post 请求中,由于某种原因,我无法获取链接到学生的班级名称以登录控制台,但 createdClass.name 会打印...

这是代码...

app.post("/students/:id", function(req, res){Student.findById(req.params.id, function(err, foundStudent){如果(错误){控制台日志(错误);} 别的 {Class.create(req.body.class, function(err, createdClass){如果(错误){控制台日志(错误);} 别的 {createdClass.student.id = foundStudent._id;createdClass.student.name = foundStudent.name;console.log(createdClass);createdClass.save();foundStudent.classes.push(createdClass);console.log(foundStudent.classes[0].name);foundStudent.save();}});}});res.redirect("/students/" + req.params.id);});

另外,这是我的模型...

学生:

var mongoose = require("mongoose");var studentSchema = new mongoose.Schema ({名称:字符串,类: [{类型:mongoose.Schema.Types.ObjectId,参考:类"}],等级:数组});module.exports = mongoose.model("Student", studentSchema);

课程:

var mongoose = require("mongoose");var classSchema = new mongoose.Schema ({名称:字符串,学生:{ID:{类型:mongoose.Schema.Types.ObjectId,参考:学生"},名称:字符串}});module.exports = mongoose.model("Class", classSchema);

在此先感谢您,如果我可以添加任何内容以使其更易于阅读,请告诉我.

这是发出帖子请求的页面...

<h1>学生档案</h1><h2>姓名:<%=student.name%></h2><div><h3>类:<form action="/students/<%= student._id %>"方法=POST"><%if(student.classes.length === 0){%><p>没有课程链接到个人资料,请添加课程..</p><input type="text" name="class[name]" placeholder="Class name"><% } else { %><% student.classes.forEach(function(course){ %><li><%= course.name %></li><% });%><%}%></表单>

解决方案

Class 是保留字,不能用于变量

I am having an issue here. I currently am making a school directory using node.js and MongoDB. I am in an app.post request and for some reason I can't get the name of the class being linked to the student to log to the console, but createdClass.name will print...

Here is the code...

app.post("/students/:id", function(req, res){
   Student.findById(req.params.id, function(err, foundStudent){
       if(err){
           console.log(err);
       } else {
          Class.create(req.body.class, function(err, createdClass){
              if(err){
                  console.log(err);
              } else {
                  createdClass.student.id = foundStudent._id;
                  createdClass.student.name = foundStudent.name;
                  console.log(createdClass);
                  createdClass.save();

                  foundStudent.classes.push(createdClass);
                  console.log(foundStudent.classes[0].name);
                  foundStudent.save();
              }
          });
       }
   });
   res.redirect("/students/" + req.params.id);
});

Also, here are my models...

STUDENT:

var mongoose = require("mongoose");

var studentSchema = new mongoose.Schema (
    {
        name: String,
        classes: [
                {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: "Class"
                }
            ],
        grades: Array
    }
);

module.exports = mongoose.model("Student", studentSchema);

CLASS:

var mongoose = require("mongoose");

var classSchema = new mongoose.Schema (
    {
        name: String,
        student: 
        {
            id: 
            {
                type: mongoose.Schema.Types.ObjectId,
                ref: "Student"
            },
            name: String
        }
    }
);

module.exports = mongoose.model("Class", classSchema);

Thank you in advance and please do let me know if there is anything I can add to make this easier to read.

Here is the page making the post request...

<div>
    <h1>Student Profile</h1>
    <h2>Name: <%=student.name%></h2>
    <div>
        <h3>Classes:
            <form action="/students/<%= student._id %>" method="POST">
                <%if(student.classes.length === 0){%>
                    <p>No classes linked to profile, please add class..</p>
                    <input type="text" name="class[name]" placeholder="Class name">
                <% } else { %>
                    <% student.classes.forEach(function(course){ %>
                        <li><%= course.name %></li>
                    <% }); %>
                <% } %>
            </form>
        </h3>
    </div>
</div>

解决方案

Class is a reserved word and can't be used for a variable

这篇关于Node.js 无法从学生模型记录类模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆