【问题标题】:Mongoose NodeJS Schema with array of ref's带有引用数组的 Mongoose NodeJS Schema
【发布时间】:2017-12-13 05:40:10
【问题描述】:

我知道有很多关于它的答案,但我还是不太明白。 我有CourseSchema:

const CourseSchema = new Schema({
course_name: String,
course_number: {type: String, unique : true },
enrolledStudents:[{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Student' }]
});

还有一个StudentSchema

const StudentSchema = new Schema({
first_name: String,
last_name: String,
enrolledCourses:[{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'CourseSchema'
    }]
});

我想将enrolledStudents 转至CourseSchema 与学生联系,并将enrolledCourses 转至StudentSchema 与课程联系。

router.post('/addStudentToCourse', function (req, res) {
Course.findById(req.params.courseId, function(err, course){
    course.enrolledStudents.push(Student.findById(req.params.studentId, function(error, student){
        student.enrolledCourses.push(course).save();
    })).save();
});
});

但发帖时出现错误:

TypeError: 无法读取属性 'enrolledStudents' of null


好的,在准备好Query-populate 之后,我做到了:

router.post('/addStudentToCourse', function (req, res) {

    Course.
    findOne({ _id : req.body.courseId }).
    populate({
        path: 'enrolledStudents'
        , match: { _id : req.body.studentId }
    }).
    exec(function (err, course) {
        if (err) return handleError(err);
        console.log('The course name is %s', course.course_name);
    });
});

当我在邮递员上点击 POST 时,我会进入控制台:

课程名称是cs的介绍

但它会永远加载到我得到的控制台上:

POST /courses/addStudentToCourse - - ms - -

【问题讨论】:

  • 在您的const CourseSchema = new ... 声明中,尝试将ref: 'Student' 更改为ref: 'StudentSchema'。不确定,但可能有效。

标签: node.js mongodb mongoose populate ref


【解决方案1】:

您缺少填充指令。例如:

see more about it here

Course.
  findOne({ courseId : req.params.courseId }).
  populate('enrolledStudents').
  exec(function (err, course) {
    if (err) return handleError(err);
    console.log('The course name is %s', course.name);

  });

它通过使用“知道”如何使用 push 语法填充 withput 的 ref 字段来工作。它就像一个外键人口。

只需在查询中调用 populate 方法,就会返回一个文档数组来代替原来的 _ids。你可以了解更多关于填充方法的内部结构in the official docs

【讨论】:

  • 好的,我取消了推送并填充,但是我如何选择 spicifice 学生?
  • 该课程有一个学生参考列表。因此,如果您在学生的参考字段中添加 id,那么它将自动为您填写本课程的相关学生。
【解决方案2】:

这就是我所做的:

router.post('/addStudentToCourse', function (req, res) {
Student.findById(req.body.studentId, function(err, student){
    if(err) return next(err);
    Course.findById(req.body.courseId, function(err, course){
        if(err) return console.log(err);
        course.enrolledStudents.push(student);
        course.save(function (err) {
            if(err)
                console.log(err);
            student.enrolledCourses.push(course);
            student.save(function (err) {
                if (err)
                    console.log(err);
                else{
                    res.send("worked");
                }
            });
        });

    });
});
});

【讨论】:

    猜你喜欢
    • 2019-03-12
    • 2019-03-27
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2021-07-31
    相关资源
    最近更新 更多