#How to writeFile with electron framework?

10 messages · Page 1 of 1 (latest)

vocal ice
#

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');
}```
lyric fiber
#

const {appendFile} = require('fs')

vocal ice
#

now i get the err: "Uncaught ReferenceError: require is not defined
at change.js:1:22"

lyric fiber
#

require is disabled in renderer for security purpose

#

use preload for it

vocal ice
#

like that: const {appendFile} = preload('fs') ?

lyric fiber
vocal ice
#

im new to coding and this step is challenging me a bit
can i show you the code in any way

lyric fiber
#

docs have a lot of examples

#

you can try but i am not giving complete code