question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: write after end when csvConverter.fromString is called two times

See original GitHub issue

My program need to parse two csv files that are stored in a json object :

csv_files = [
               {
                  "name" : "<name>",
                  "file" : "./file.csv"
               },
               [...]
           ]

I use async to execute some part of my code synchronously. And in my main program I read all file from csv_files in order to parse all csv.

If I call one time csvConverter.fromString it’s works perfectly. But when I want to do a loop (something like that) I have Error: write after end.

With this code :

[...]
        allCsv2bdd_url(csv_files[i], function(err){
            if(csv_files.length > i){
                i++;
                allCsv2bdd_url(csv_files[i], function(err){});
            }else {
                done();
            }
        });

I have the error message.

In allCsv2bdd_url I execute two functions in synchronous mode, including the csvtojson function.

    var csvLines = '';

    //- Read Lines
    try{
        fs.readFileSync(csv_file).toString().split('\n').forEach(function (line) {
            csvLines += line;
            console.log(' [url.csv] '+line);
        });
        //- Transform lines to json
        csvConverter.fromString(csvLines, function(err, jsonObj){
            if (err){
                console.log('\n [url.csv] An error as occured when trying to convert csv to JSON :\n\t'+err);
                callback( );
            } else {
                callback( jsonObj );
            }
        });
    }catch(e){
        console.log('\n [url.csv] /!\\ Cannot access to file '+csv_file+' with the following error :\n\t'+e);
        callback( );
    }

Is someone can explain why I have this problem ?

Thanks 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
Keyangcommented, Apr 21, 2016

Hi, You have to create new Converter instance for different files. Converter is based on stream. Once a file processing finished, the stream is closed. To process another file, create new converter.

~Keyang

On 21 April 2016 at 09:38, flexbrane notifications@github.com wrote:

I tried to slightly modify my code :

try{
    //- Transform file lines to json
    csvConverter.fromFile(csv_file,function(err,jsonObj){
        if (err){
            console.log('\n [url.csv] An error as occured when trying to convert csv to JSON :\n\t'+err);
            callback( );
        } else {
            callback( jsonObj );
        }
    });
}catch(e){
    console.log('\n [url.csv] /!\\ Cannot access to file '+csv_file+' with the following error :\n\t'+e);
    callback( );
}

Same error

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/Keyang/node-csvtojson/issues/89#issuecomment-212810235

1reaction
Keyangcommented, Nov 1, 2016

Thanks. I will make this better in next version.

~Keyang

On 1 November 2016 at 15:58, Dolan notifications@github.com wrote:

I had the same issue and took me 3 hours to figure out the problem

I feel this needs to be more clear, a more descriptive error than:

“write after end”

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Keyang/node-csvtojson/issues/89#issuecomment-257605933, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCs93Ibpg-a-NeMyM4G-3NqCTXzFJzEks5q52GOgaJpZM4IMaRb .

Read more comments on GitHub >

github_iconTop Results From Across the Web

'Write after end' error with Express, csvtojson and node-walk
This assumes that data passed in by the csv converter on 'end_parsed' is always an array. It combines the data from all files...
Read more >
Writing to Files in Node.js - Stack Abuse
write when performing fine-grained updates to files, for instance, writing a sequence of bytes at a known position in the middle of a...
Read more >
Top 10 Mistakes Node.js Developers Make - AirPair
1 Not using development tools. 1.1 Automating restarts; 1.2 Automatic browser refresh · 2 Blocking the event loop · 3 Executing a callback ......
Read more >
Top 10 Most Common Node.js Developer Mistakes - Toptal
Regardless of how difficult Node.js makes writing safe code, and how easy it makes ... deals with such a scenario, “done” may be...
Read more >
Understanding Streams in Node.js - NodeSource
Streams are a way to handle reading/writing files, ... Handle stream events --> data, end, and error ... Like in the following example:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found