javascript - 我如何使用 Express/Mongoose 将模型引用到我的用户模型

标签 javascript node.js rest express mongoose

我有两个模型,一个是我的用户模型,另一个是我的类(class)模型。我希望拥有它,以便当用户(教师)创建类(class)时,它将该类(class)分配给他们,反之亦然。以下是我的模型,可以更好地解释:

类(class)架构/模型:

var CourseSchema = new Schema({
    courseID: {
        type: Number,
        unique: true
    },
    courseName: String,
    courseDesc: {
        type: String,
        default: "No course description provided."
    },
    coursePicture: {
        type: String,
        required: false
    },
    teacher: [
        {
           type: mongoose.Schema.Types.ObjectId,
           ref: 'User'
        }
    ],
    students: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Student'
         }
    ]
})

用户架构/模型:

var UserSchema = new mongoose.Schema({  
  firstName: String,
  lastName: String,
  email: String,
  courses: [
    {
       type: mongoose.Schema.Types.ObjectId,
       ref: 'Course'
    }
  ], 
  password: String
});

基本上,我希望在我的前端拥有它,我可以做 course.teacher.firstName 或 user.courses 之类的事情。我的模式在两个不同的文件中,但我认为这很好。这就像在用户创建帖子时为其分配帖子。我不知道我该怎么做,因为我已经尝试了很多事情。

现在,我正在使用它来创建类(class)。

// Creates a new course
router.post('/create', function (req, res) {
    Course.create({
            courseID : req.body.courseID,
            courseName : req.body.courseName,
            courseDesc : req.body.courseDesc,
            coursePicture : req.body.coursePicture,
            teacher : req.body.id,
            students: req.body.students
        }, 
        function (err, course) {
            if (err) return res.status(500).send("There was a problem adding the information to the database.");
            res.status(200).send(course);

        });
});

我已经在代码 ^ 所属的 Controller 中引用了用户模型,因此 var User = require('../user/User'); 我相信这是实现这一目标所必需的。如果您有任何疑问,请告诉我,因为我不是最擅长解释此类事情的人。

希望有人能帮帮我!

谢谢。

最佳答案

// Creates a new course
router.post('/create', function (req, res) {
    Course.create({
            courseID : req.body.courseID,
            courseName : req.body.courseName,
            courseDesc : req.body.courseDesc,
            coursePicture : req.body.coursePicture,
            teacher : req.body.id, // find this user
            students: req.body.students,
            attendance: req.body.attendance 
        }, 
        function (err, course) {
            User.findById(req.body.id, function(err, user) {
                user.update({
                    $push: {
                        courses: course._id
                    }
                }, function(err) {
                     if (err) return res.status(500).send("There was a problem adding the information to the database.");
                     res.status(200).send(course);
                })
            })
        });
});

关于javascript - 我如何使用 Express/Mongoose 将模型引用到我的用户模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50611692/

相关文章:

c# - 异步 WCF REST 服务中跨线程的上下文

xss - 应该在 API 或客户端级别对输出进行编码吗?

Javascript:将var中的文本解析为一个集合

c++ - 使 C++ 插件异步

javascript - 在 Firestore 中创建子集合会生成两个具有相同 id 的文档(而不是更新预先存在的文档)

.net - rabbitmq 的 REST API

javascript - 如何在此 super 代理调用中使用 async/await?

Javascript 智能舍入

javascript - 如何使用 Google Maps API v3 突出显示地址?

node.js - API Gateway 排序查询字符串