I need to read a text file and pass data as object to another function in vue2. The uploadBulk function reads the text file and submitBulk() will be executed only after finished reading the file. But now submitBulk() is being executed before and while i printing obj inside submitBulk its returning an error that the obj is undefiend or null. I have the following code-
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()
}
};
}
}