I have two schemas Teacher and Student
StudentSchema = new mongoose.Schema({
email:{type:String, index: {unique:true}}
name:{type:String},
marks:[{
subject:{type:String,
marks:{type:Number}
}]
})
TeacherSchema = new mongoose.Schema({
email:{type:String, index: {unique:true}}
name:{type:String},
students:[{
email:{type:String},
registerationDate:{type:Date}
}]
})
I have an API where I get teacher's email id and have to respond with marks and name of the students registered to that particular teacher.
For this, I'm using this code
var teacher = await Teacher.findOne({"email":req.body.email})
teacher.students.forEach(function(students){
let student = Student.findOne({"email":students.email})
console.log(student) // to watch the result
})
I want to get the complete Student schema in my student variable so that I can use the data of the students.
But I'm not getting the desired output because I can't user await along with Student.findOne.
Like this
let student = await Student.findOne({"email":students.email})
I'm getting a Query object as a result.
Can anyone suggest any way to use await in the loop or any other way to get my desired output?
Node crashes if I use await anywhere inside my loop, so solutions answered elsewhere to use async/await in loop is not solving my problem.