Я хочу объединить несколько частей данных из нескольких запросов в один переменный массив, но при этом он не будет ждать, пока данные будут помещены в массив, и напрямую печатает последний консольный журнал.
Я использую пн goose для запросов.
graph_con.getScoreCard = (studId)=>{
Data.find({student:studId})
.distinct('category')
.lean()
.exec((err,obj)=>{
if(err) throw err;
getAverageByCategory(obj,studId)
})
async function getAverageByCategory (obj,studId){
var data = []
for (let i = 0; i < obj.length; i++) {
Data.find({student:studId,category:obj[i]})
.lean()
.distinct('difficulty')
.exec((err,doc)=>{
if(err) throw err;
getAverageByDifficultiy(doc,studId)
.then((val)=>{
data.push{{
data:val,
category:obj[i]
}}
console.log(data); //this print the data but one by one
})
})
console.log(data); // this is null, I want all data here and return that data
}
}
async function getAverageByDifficultiy (obj,studId){
var d =[]
for(let j = 0;j<obj.length;j++){
// const rightAns = await Data.find({student:studId,difficulty:obj[j],result:true,gameComplete:true}).lean().countDocuments({});
// const totalquestion = await Data.find({student:studId,difficulty:obj[j],gameComplete:true}).lean().countDocuments({});
const rightAns = await Data.find({student:studId,difficulty:obj[j],result:true}).lean().countDocuments({});
const totalquestion = await Data.find({student:studId,difficulty:obj[j]}).lean().countDocuments({});
// console.log( "Number of right:", rightAns);
// console.log( "Number of totalquestion:", totalquestion);
if(totalquestion ==0){
d.push({msg:'nodata'});
return d;
}
var score = ((rightAns*2)/(totalquestion*2))*100
var data = {
difficulty:obj[j],
percent:score
}
d.push(data);
//console.log(data);
}
return d;
}
}