Hello! Here's what my app does:
By clicking a button, it reads the content of a .txt-File by using a httprequest. It changes one specific character and saves the changed content to a variable.
This variable should be saved in the same .txt-File. How can I do that?
If i want to use import { appendFile } from 'fs';, i get the error: "Uncaught SyntaxError: Cannot use import statement outside a module".
Thanks for your help! 🙂
Here`s my code:
function C (name, status){
this.name = name,
this.status = status
}
function change(id){
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "champs.txt", true);
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4) {
var allText = xhttp.responseText;
const champArray = allText.split('\n');
var list = [];
for (let i = 0; i < champArray.length; i++){
let currentChamp = champArray[i].split(', ');
const c = new C(currentChamp[0], currentChamp[1]);
list.push(c);
}
let currentObeject = list.find(i => i.name == id);
if (currentObeject.status.slice(0,-1) === "done"){
currentObeject.status = "open";
}else{
currentObeject.status = "done";
}
const index = list.findIndex(currentObeject => currentObeject.name === id);
list[index] = currentObeject;
var backArray = [];
for (j = 0; j<list.length; j++){
var k = list[j];
backArray[j] = k.name + ", " + k.status;
}
var newAllText = backArray.join('\n');
console.log(newAllText);/*
fs.writeFile('champs.txt', newAllText, err => { if(err){ console.err; return } });
fs.close();*/
/*fs.appendFile('champs.txt', newAllText, function (err) {
if (err) throw err;
console.log('Saved!');
});*/
}
}
//console.log(allText);
xhttp.send('filler');
}```