ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

Writing to csv-file not working, but no error message appearing #45

Closed dercodeling closed 4 years ago

dercodeling commented 4 years ago

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.

dercodeling commented 4 years ago

Because it works when reloading I am thinking it might be a bug, as the inputs don't change.

ryu1kn commented 4 years ago

Hi @Dr747 , have you looked at what you get in .catch(?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

You can use like this:

csvWriter.writeRecords(settings)
    .then(() => {
        console.log('Saved succesfully!');
    })
    .catch(e => {
        console.error('Error occurred!', e.stack);
    });
dercodeling commented 4 years ago

Yes, I have .chatch in place, I forgot mentioning that. Strangely, there is no error message whatsoever.

dercodeling commented 4 years ago

I just ran it for the first time in hours and it worked a few times, then when I reloaded it stopped working again. The same thing happened yesterday too.

ryu1kn commented 4 years ago

Hmm... can you somehow find a log? Without a log, debugging is hard.

csv-writer essentially converts js objects (objects/arrays) into strings, and write them to a file. If you cannot find a log, I would put a raw file write, e.g. fs.writeFile('path/to/file.csv', 'this,is,a,test,csv', 'utf8') instead of csv-writer and wait for few hours and see if it can face the same issue.

dercodeling commented 4 years ago

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 for all the attempts of that session at once, and the file was written. 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 am aware that there is not much to do without a log, I don't know though where to find one or how to make one.

dercodeling commented 4 years ago

It also seems to always work on startup or when restarted with rs and when reloaded, e.g. with ctrl+r, it stops working.

ryu1kn commented 4 years ago

Thanks for the additional info, with that we now know the problem seems to be at the call of Node's writeFile.

it sent errors for all the attempts of that session at once

What error are you getting here?

Also, if you can do console.log, can you try this?

try {
  fs.writeFile('path/to/file.csv', 'this,is,a,test,csv', 'utf8', err => {
    if (err) {
      console.log('Error captured in writeFile callback')
      console.log(err.stack)
    }
  })
} catch (err) {
  console.log('Error captured in try/catch block')
  console.log(err.stack)
}

It's unlikely that writeFile throws an exception and it's captured in try/catch, but maybe in your environment, writeFile can be wrapped in something (i.e. just in case).

dercodeling commented 4 years ago

Originally I was getting the console.log('Hello World > helloworld.txt' message, which I just now noticed.

With that try ... catch code, I get the exact same problem as with the csv-writer. File empties, no error whatsoever, file gets written when reloading.

ryu1kn commented 4 years ago

ok... sorry I don't think I can help you much on this without log. The problem occurs with just a plain fs.writeFile call; so it doesn't seem to be related to csv-writer. Maybe you can ask someone in the electron context...

I'm closing this for now.

ralyodio commented 4 years ago

has anyone resolved this? the readme example isn't working for me either. I get no errors. and an empty csv file.

dercodeling commented 4 years ago

In the end I somehow got it to work with manually parsing the Array to a String and then using fs.writeFile or writeFileSync. Basically this as in the answer by @ryu1kn :

try {
  fs.writeFile('path/to/file.csv', 'this,is,a,test,csv', 'utf8', err => {
    if (err) {
      console.log('Error captured in writeFile callback')
      console.log(err.stack)
    }
  })
} catch (err) {
  console.log('Error captured in try/catch block')
  console.log(err.stack)
}
ralyodio commented 4 years ago

in my case i had to do await writeCsv()