如何使用Nodejs mongodb和angularjs检查注册课程数组中是否存在特定课程

如何使用Nodejs mongodb和angularjs检查注册课程数组中是否存在特定课程,angularjs,node.js,mongodb,Angularjs,Node.js,Mongodb,我正在尝试创建一个课程注册门户。一切正常,但我希望有一种方法可以阻止:一名学生一遍又一遍地注册同一门课程这是我的代码: FIRST I CREATE A STUDENT SCHEMA var studentSchema = new Schema({ firstname:{type: String, required: true}, middlename:{type: String, required: true}, lastname:{type: String, requ

我正在尝试创建一个课程注册门户。一切正常,但我希望有一种方法可以阻止:一名学生一遍又一遍地注册同一门课程这是我的代码:

FIRST I CREATE A STUDENT SCHEMA
var studentSchema = new Schema({
    firstname:{type: String, required: true},
    middlename:{type: String, required: true},
    lastname:{type: String, required: true},
    department:{ type: Schema.Types.ObjectId, ref: 'department', required: true },
    Courses:[{type: Schema.Types.ObjectId, ref: 'course'}],
  });
学生模式有一个子文档Courses,它是courseSchema的数组

var courseSchema = new Schema({
   title: {type: String, required: true},
   code:{type: String, required: true, unique: true},
   unit:{type: String, required: true},
   level:{type: String, required: true},
   department:{type: Schema.Types.ObjectId, ref: 'department'}
});
我还创建了一个API来获取mongodb中的所有课程,其中student.department等于course.department这是代码

router.get('/student/getcourses/:studentid', function(req, res){
if (!req.params.studentid) {
    sendJSONresponse(res, 404, {
        "message": "Not found, studentid is required"
    });
        return;
}
Student
.findOne({_id : req.params.studentid})
.exec(function(err, student){
    if (err) {
        sendJSONresponse(res, 404, err);
    }else{
        Course
        .find({department : student.department})
        .populate('department')
        .exec(function(err, courses){
            if (err) {
                sendJSONresponse(res, 404, err);
            }else{
                sendJSONresponse(res, 200, courses);
            }
        });
    }
 });
});
router.post('/student/courseReg/:studentid', function(req, res){
if (!req.params.studentid) {
    sendJSONresponse(res, 404, {
        "message": "Not found, studentid is required"
    });
        return;
}
Student
.findByIdAndUpdate(req.params.studentid, {$push: {regCourses:req.body.course}})
.exec(function(err, student){
    if (err) {
        sendJSONresponse(res, 404, err);
    }else{
        sendJSONresponse(res, 200, " registration successfull");
    }
 });
});
我还创建了一个API,它将为学生注册一个特定的课程。下面是代码

router.get('/student/getcourses/:studentid', function(req, res){
if (!req.params.studentid) {
    sendJSONresponse(res, 404, {
        "message": "Not found, studentid is required"
    });
        return;
}
Student
.findOne({_id : req.params.studentid})
.exec(function(err, student){
    if (err) {
        sendJSONresponse(res, 404, err);
    }else{
        Course
        .find({department : student.department})
        .populate('department')
        .exec(function(err, courses){
            if (err) {
                sendJSONresponse(res, 404, err);
            }else{
                sendJSONresponse(res, 200, courses);
            }
        });
    }
 });
});
router.post('/student/courseReg/:studentid', function(req, res){
if (!req.params.studentid) {
    sendJSONresponse(res, 404, {
        "message": "Not found, studentid is required"
    });
        return;
}
Student
.findByIdAndUpdate(req.params.studentid, {$push: {regCourses:req.body.course}})
.exec(function(err, student){
    if (err) {
        sendJSONresponse(res, 404, err);
    }else{
        sendJSONresponse(res, 200, " registration successfull");
    }
 });
});
在angularjs的前端,我就是这么做的

$http.get('/api/student/getcourses/' + $scope.studentid)
.success(function(data) {
    $scope.courses = data ; 
})
.error(function (e) {
    return e;
});
$scope.register = function(course){
            $http.post('/api/student/courseReg/' + $scope.studentid, {course: course})
            .success(function(data){
                console.log(data);
            });
};
这是html代码

 <div class="table-responsive">
     <table class="table"> 
         <thead> 
             <tr> 
                 <th>Title</th> 
                 <th>Code</th> 
                 <th>Department</th> 
                 <th>level</th>                      
             </tr> 
         </thead> 
             <tbody> 
                 <tr ng-repeat="course in courses |  filter: adminfilter"> 
                     <td>{{course.name}}</td> 
                     <td>{{course.code}}</td> 
                     <td>{{course.department.name}}</td> 
                     <td>{{course.level}}</td> 
                     <td>
                        <button  ng-click="register(course._id)"  class="btn btn-info btn-xs">Register</button>
                     </td>                      
                   </tr> 
             </tbody>
         </table>
      </div

标题
代码
部门
水平仪
{{course.name}
{{course.code}
{{课程.部门.名称}
{{course.level}}
登记

如果学生已注册,则需要为每门课程添加信息:

课程
.find({系:学生.系})
.populate('部门')
.exec(函数(错误,课程){
如果(错误){
sendJSONresponse(res,404,err);
}否则{
课程。forEach(课程=>{
course.isRegistered=student.Courses.indexOf(course.\u id)>-1;
}
sendJSONresponse(res,200,课程);
}

});
如果学生已经注册了每门课程,则需要为每门课程添加信息:

课程
.find({系:学生.系})
.populate('部门')
.exec(函数(错误,课程){
如果(错误){
sendJSONresponse(res,404,err);
}否则{
课程。forEach(课程=>{
course.isRegistered=student.Courses.indexOf(course.\u id)>-1;
}
sendJSONresponse(res,200,课程);
}

})
很抱歉,我不太理解你的代码,你能给我一个关于我发布的内容的清晰代码吗?这样我就可以看到我的代码哪里出错了,你到底不明白什么?第一个是当用户要求提供课程列表时。第二个是你的表格按钮。第三个是你使用findByIdA的地方Ndupdate对不起,我真的不明白你的代码,你能给我一个关于我发布的内容的清晰代码吗?这样我就可以看到我的代码哪里出错了,你到底不明白什么?第一个是当用户要求提供课程列表时。第二个是你表格上的按钮。第三个是你使用findByIdA的地方更新