Hey everyone, I could really use your help right now. Here's the code block I'm struggling with:
function readyToSubmit(answerPack, answerArr, len) {
for (var i = 0; i < answerArr.length; i++) {
var questionId = answerArr[i].id;
console.log(questionId);
// This is an asynchronous database operation
userStore.getDoc(id).then(function(doc) {
// When I try to log 'answerArr[i]' here, it shows up as undefined
// I understand that it's because 'i' here refers to answerArr.length, so it ends up undefined
// I want each questionId to be different, but it always ends up being the last one in the array
// I know this is a closure issue, but I'm unsure how to solve it.
doc.questionId = questionId; // always ends up being the same one
answerPack.push(doc);
});
}
}
Can anyone guide me on how to achieve what I need in each iteration? I really appreciate any assistance you can provide. Thanks a lot! :)