我的回调行为与预期的节点js不一样

时间:2018-12-26 02:24:06

标签: javascript node.js mongoose callback mean-stack

我要做的是首先从学生集中收集所有特定年级和班级的学生。然后使用该请求的年级和班级中的学生ID,使用猫鼬

从Marks集合中使用这些学生ID查找标记。

这是我的学生模式

const StudentSchema = new Schema({
    base_no:{
        type: String,
        required: true 
    },
grade: {
        type: Number,
        required: false
    },
    class: {
        type: String,
        required: false
    }

});

const Student = module.exports = mongoose.model('student',StudentSchema,'student');

然后我在学生模型中有一个这样的方法,可以让所有处于特定年级和特定班级的学生获得

/**
 *  Get all students of a class for a given grade
 * 
 */
module.exports.getStudentsGradeClass = function(params,callback){
    Student.find({grade: params.grade, class: params.class},'base_no first_name last_name').sort({base_no: 1}).exec(callback);
}

这是我的标记架构

 const MarksSchema = new Schema({

        verifire: {
            type: String,
            required: true,
            //index:true
        },
        student_base_id: { // as student auto gen key/index no
            type: String,
            required: true,
            //index:true
        },
        subject_id: {
            type: String,
            required: true,
            ref: 'subject',
            //index:true
        },
        total: {
            type: Number,
            required: true
        }
    });


    const Marks = module.exports = mongoose.model('school_marks', MarksSchema, 'school_marks');

所以我正在使用以上学生模式的 getStudentsGradeClass() 从客户端获取请求的学生的方法 然后使用此代码使用getStudentsGradeClass()方法中的Student ID从Marks模式中获得分数

    /**
         *  Get marks of all students of the requested grade->class with subject_id
         * 
         */
        module.exports.getClassResults = function (params, callback) {
            let marks = [];
            Student.getStudentsGradeClass({ grade: params.grade, class: params.class }, (err, students) => {
                if (!err) {
                    for (let i = 0; i < students.length; i++) {
                        Marks.find({ subject_id: params.subject_id, student_base_id: students[i].base_no }, 'student_base_id total', (err, data) => {
                            if (!err) {
                                marks.push(data[0]);
                            } else {
                                callback(err, null);
                            }
                        });
                    }
                    callback(null,marks); // this is the function I wan to invoke
                                          // right after the for loop finishes so 
                                          // that i can get all marks of all 
                                          //students for a specific subject..   


    // currently it is undefined(marks[]) coz it goes straight for the callback 
     //  giving control to the for loop, the for loop gives data much later so that 
    // marks are not pushed to the array when I call it.

                } else {
                    callback(err, null);
                }

            });
        }

所以我可以这样做吗(我知道我身处厄运金字塔中)。请为我建议一种更清洁的方法,或者您可以指导我克服此问题或如何实现我想要达到的目标。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用异步/等待...........

 module.exports.getClassResults =  (params, callback)=> {
            let marks = [];
            Student.getStudentsGradeClass({ grade: params.grade, class: params.class },async (err, students) => {
                if (!err) {

                 for (let student of students){
                      let data=await Marks.find({ subject_id: params.subject_id, student_base_id: student.base_no }, 'student_base_id total')
                            if (data) {
                                marks.push(data[0]);

                            } else {
                                callback(err, null);
                            }

                    }
                    callback(null,marks); // this is the function I wan to invoke
                                          // right after the for loop finishes so 
                                          // that i can get all marks of all 
                                          //students for a specific subject..   


    // currently it is undefined(marks[]) coz it goes straight for the callback 
     //  giving control to the for loop, the for loop gives data much later so that 
    // marks are not pushed to the array when I call it.

                } else {
                    callback(err, null);
                }

            });
        }