I am writing a function in expressJs with database calls too but I am unable to understand why even though async await is used the following statement is giving 0 value: In this console statement countRight, countWrong is giving value as zero.
console.log("End...", countRight, countWrong);
The code block is below:
exports.finishQuiz = async (req) => {
let eid = req.params.quizId;
const {selectedAnswers} = req.body;
let countRight = 0;
let countWrong = 0;
let answer;
console.log("Start...");
selectedAnswers.forEach(async function(el) {
answer = await answerModal.findOne({
where :{qid: el.qid}
});
answer = answer.toJSON().answerValue;
// counter to calculate right and wrong.
if(el.answerSelected.trim() === answer.trim()){
countRight = countRight + 1;
}
else{
countWrong = countWrong + 1;
}
});
// calculate number of right answer
// and wrong answer
console.log("End...", countRight, countWrong);
}
I am confused completely why the values are not coming.