I am facing an issue where the submitBulk() function is being executed before the uploadBulk function finishes reading a text file and passing data as an object to another function in vue2. When I print obj inside submitBulk, it returns an error stating that obj is undefined or null. Below is the code I am using:
uploadBulk: function (){
if (this.qCategory == 'MCQ') {
var questions = []
var file = this.$refs.uploadedFile.files[0];
if (file) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = () => {
/// mcq
var str = reader.result;
const obj = str.split(/\n/u).reduce((acc, line, i) => {
if (i%2===0) acc.questions.push({"body":line.match(/\(.+\)(.*)/u)[1].trim()}); // remove the (X) from the question
else {
const curItem = acc.questions[acc.questions.length - 1]; // last pushed object
let [optionStr, answer] = line.split(/। /u);// split on this special character
// assuming 4 options
curItem.options = optionStr
.match(/\(.+\) (.+) \(.+\) (.+) \(.+\) (.+) \(.+\) (.+)/u)
.slice(1); // drop the first element from the result (full match)
answer = answer.match(/\((.+)\)/u)[1]; // just get the letter from the bracket
curItem.answers = [answer];
curItem.type = "MCQ"
}
return acc
}, {questions: []})
this.submitBulk()
}
};
}
}