mholt / PapaParse

Fast and powerful CSV (delimited text) parser that gracefully handles large files and malformed input
http://PapaParse.com
MIT License
12.54k stars 1.15k forks source link

Complete called two times #231

Closed acourtiol closed 4 years ago

acourtiol commented 9 years ago

Hello,

I'm having the following issue.

When I call papaparse like this:

Papa.parse(data.toString('utf8'), {
    header: true,
    skipEmptyLines: true,
    complete: function(data) {
         console.log(data);
}});

With the following string:

Identifiant,Nom
1,Succes commerciaux
2,Strategie commerciale
3,Synergies
4,Efficacite operationnelle
5,Numerique
6,3 questions a

The complete function get called two times in a row with the following data:

{ data: 
   [ { Identifiant: '1', Nom: 'Succes commerciaux' },
     { Identifiant: '2', Nom: 'Strategie commerciale' },
     { Identifiant: '3', Nom: 'Synergies' },
     { Identifiant: '4', Nom: 'Efficacite operationnelle' },
     { Identifiant: '5', Nom: 'Numerique' },
     { Identifiant: '6', Nom: '3 questions a' } ],
  errors: 
   [ { type: 'Delimiter',
       code: 'UndetectableDelimiter',
       message: 'Unable to auto-detect delimiting character; defaulted to \',\'',
       row: undefined } ],
  meta: 
   { delimiter: ',',
     linebreak: '\n',
     aborted: false,
     truncated: false,
     fields: [ 'Identifiant', 'Nom' ] } }

I parse multiple files but I'm having the issue only on this one.

Thanks.

mholt commented 9 years ago

This is a known issue. I'm mobile right now and can't remember which number, (and my Internet is down) but take a look at the open issues and you will find the previous discussion.

acourtiol commented 9 years ago

@mholt Thanks for the answer, I searched through the search tool but didn't find anything like that. I'll double check.

AVermeij commented 9 years ago

I seem to be having the exact same problem: as soon as I load up a second file in an input file element, PapaParse somehow calls the complete two times. My code:

$('input').change(function(event) {
  file = event.target.files[0];
});

Papa.parse(file, {
        header: true,
        dynamicTyping: true,
        skipEmptyLines: true,
    encoding: 'UTF-8',
    beforeFirstChunk: function(chunk) {
        return chunk.replace(regex, '');
    },
        complete: function(results) {
               //doing some stuff with results.data
        }

Tried to find the related issue, but couldn't locate it. Would you be so kind to refer me to it? Thanks for your great work :-)

AVermeij commented 9 years ago

I just found out that this has nothing to do with Papa in the end ;-). My form was submitting multiple times because I didn't detach it before attaching it. This fixed it:

$('#form').off('submit').on('submit', function(e) {
// do stuff
}

Hope this helps anyone with the same issue!

renak commented 9 years ago

I'm having the same issue. I'm starting the Papa.parse on file input change event. The chunk function is only run once (testing with a small file), but the complete function is run twice.

bluej100 commented 9 years ago

I'm having trouble reproducing this. Are you guys running 4.1.2?

renak commented 9 years ago

Yeah, running 4.1.2. I just realized that this only happens for me when parser.abort() is called from within the chunk function.

mholt commented 9 years ago

I'll try to take a look at this soon. (got quite the backlog!)

themeteorchef commented 9 years ago

Any update on this?

derouck commented 8 years ago

i'm seeing this issue as well.

mholt commented 8 years ago

I'm swamped with another project. Anyone is welcome to issue a PR to fix it.

repjackson commented 8 years ago

I'm seeing this. The parsing runs once (confirmed by a console.log) but the complete function runs twice. I'll see what I can find in the code. @mholt Any suggests where to look would be appreciated. And thanks for the great package, it's a huge help for my project.

mholt commented 8 years ago

@repjackson Sorry, I'm super busy right now with other things, but using the debugging tools and some console.logs you or someone can probably pinpoint the cause without too much trouble!

repjackson commented 8 years ago

Ok thank you for the quick response.

On 3/10/16, Matt Holt notifications@github.com wrote:

@repjackson Sorry, I'm super busy right now with other things, but using the debugging tools and some console.logs you or someone can probably pinpoint the cause without too much trouble!


Reply to this email directly or view it on GitHub: https://github.com/mholt/PapaParse/issues/231#issuecomment-195170325

matteolefthanders commented 8 years ago

i'm seeing this issue right now

repjackson commented 8 years ago

I got around this by doing:

                secondIteration = false
                Papa.parse csvToParse,
                    header: true
                    complete: (results, file) ->
                        if secondIteration then return
                        else
                         // code
                            secondIteration = true
gabegorelick commented 6 years ago

See https://github.com/mholt/PapaParse/pull/479#issuecomment-379918392 for a partial solution that can be expanded if people are still experiencing this.