gpt4 book ai didi

javascript - 我的回调没有像预期的那样运行 node js

转载 作者:行者123 更新时间:2023-11-30 19:56:31 25 4
gpt4 key购买 nike

我想做的是首先从学生集合中获取特定年级和类(class)的所有学生。然后使用请求的年级和类(class)中的学生 ID,使用 mongoose 从 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');

然后我在学生模型中有一个这样的方法来获取特定年级和特定类(class)的所有学生

/**
* 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');

所以我正在使用上面的 Student Schema 的 getStudentsGradeClass()从客户端获取请求学生的方法然后使用此代码使用来自 getStudentsGradeClass() 方法的学生 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);
}

});
}

那么我可以这样做吗(我知道我在厄运金字塔中)。请给我一个更简洁的方法来做到这一点,或者你能指导我克服这个问题或者如何实现我想要达到的目标。非常感谢任何帮助。

最佳答案

你可以试试 async/await......

 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);
}

});
}

关于javascript - 我的回调没有像预期的那样运行 node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53926865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com