Here i am iterating an array and save it i database. After completion of iteration, i have to send the response to front end only once(not inside the loop), here is my code. I am declaring a boolean called create quest to false; after iterating all the element in array i have to return response by checking boolean if true? But before completing the loop, below if condition is executing/ so that time boolean is still false; after that line number 9 will start executing.
createQuest = false;
questionList.forEach(element => {
var quest = new Questionnaire({
_id: new mongoose.Types.ObjectId(),
question: element.question,
blockId: blockId
});
quest.save((err1, doc1) => {
9: if(!err1){
createQuest = true;
}else{
res.send({'message':'failed'});
}
})
});
if(createQuest == true){
res.send({'message':'success'});
}