2

I am trying to write to a CSV-file from my electron renderer.js process. Unfortunately, most of the time the writing isn't working, and the .then addition isn't executed. Strangely, there is no kind of error message or anything that would tell me my mistake. Some few times it has worked though, the file was written but the confirmation from .then wasn't displayed in the console. I don't have any clue as to what went differently these times.

When reloading the application with ctrl+r after the failed attempt the saving process is run again (somehow the onclick attribute of a button, containing a function call for the function all this stuff here ↓ is in) and that always works (including the .then call).

My code:

var settings = [
    ["devtools", false, devtools, ""],
    ["language", "en", language, ""]
]
var csvWriter = window.createCsvWriter({
    header: ['ABBREVIATION', 'DEFAULT', 'CUSTOM', ''],
    path: 'saves/settings.csv'
});

csvWriter.writeRecords(settings)
    .then(() => {
        console.log('Saved succesfully!');
    });

window.createCsvWriter is a preloaded script, devtools and language are script wide variables that are updated shortly before this.

I have no idea where the error is coming from, as it can't be in the path or something like that as it has run successfully multiple times. I even tried to follow the debugging process line by line, but all I think I have found out is that the settings array is fully dealt with, the script ends somewhere in a jungle of loops and if-clauses concerning the path or something. I also know that a normal CSV-file wouldn't have a comma on the end of the rows, my code importing the settings later just can't deal with that, which I will fix later. If you need any more information just ask.

EDIT: I just followed the code again line by line and notices that it stops after the return __awaiter() in CsvWriter.prptotype.writeRecords = function (records) {...}. records is an array with the correct data for the CSV. Maybe that is useful information.

EDIT2: I tried using fs.writeFile() to write and it has the same problem, the file became empty and there was no error. I noticed though, that when reloading (remember originally it worked when reloading) it sent errors or confirmations for all the attempts of that session at once, and the file was written (if there were confirmations). So I assume the problem is something that stops the code from fully running until the page is reloaded. Any ideas what that could be? I imagine it's possible that it's another script or something global.

I don't have any breakpoints stopping the code.

2
  • Don't know – but which console are you looking at? The render processes have their own console connected to the window. Commented Jun 3, 2020 at 16:33
  • Neither the VSCode console nor the one inside the electron window have any messages inside. Most other errors appeared in the electron one, so I expected it to be there Commented Jun 3, 2020 at 17:00

1 Answer 1

1

writeRecords returns a promise that is either resolved or rejected. You code only handles the 'resolved' path and you ignore any rejection. Try this, it could reveal the error:

csvWriter.writeRecords(settings)
    .then(() => {
        console.log('Saved succesfully!');
    })
    .catch((err) => {
        console.log('Save failed', err);
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I forgot the catch part. Sadly, it still doesn't always work and there is also no error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.