an xml document is edited using a function. First, all duplicate files are removed. Then the content is entered into a sql database. For this I run a loop that corresponds to the length of the xml elements (here already converted to json). When I look at the content of the table in the database, I see that the order of the entries does not correspond to the order of the array. So within the database query of my server I printed the run variable i and saw that it starts at 0 but 2,3,1 are appended to the end in this order. Unfortunately I don't understand at all why this happens and hope that someone can help me here.
function extractquestions(filename){
let questions = [];
let sql_insert = 'INSERT INTO Fragen (Titel, Frage) values (?,?)';
fs.readFile('./files/'+filename, function(err, data){
if(err){
throw err;
}
const xmlDataStr = data;
const options = {
ignoreAttributes: false,
attributeNamePrefix : "attr_"
};
const parser = new XMLParser(options);
const output = parser.parse(xmlDataStr);
for(let i = 0; i<output.quiz.question.length; i++){
//Here the needed Questions are filtered out of the xml (Json) data
if(output.quiz.question[i].attr_type == "coderunner"){
questions.push(output.quiz.question[i]);
}
}
//Delete Duplicates from array
const filteredQuestions = questions.reduce((acc, current) => {
const x = acc.find(item => item.name.text === current.name.text);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
for(let i = 0; i<filteredQuestions.length; i++){
db.run(sql_insert, filteredQuestions[i].name.text, filteredQuestions[i], (err, result)=>{
console.log(i);
if(err){
throw err;
}
});
}
});
}