Vahe Akhsakhalyan
Vahe Akhsakhalyan

Reputation: 2186

CSV file read and uploaded

I have chosen the file from form. and in nodejs part I have create the Busboy and have this part of code:

 busboy.on('file', function(filn, file) {
    file.on('data', function(data) {
    csvParse(data, function(e, d) {
    .... here is going some logic
    and
    ...
    fs.writeFileSync(f, data_)
    })
    })


    }) 
.on('end', function() {
fs.closeSync(f)
})

So, now I have noticed that the 'end' is fired until csvParse callback comes back. csvParse in require('csv-parse'). How can solve this problem

Upvotes: 0

Views: 220

Answers (1)

Rahul Kumar
Rahul Kumar

Reputation: 144

Try this way.

    busboy.on('file', function (filn, file) {
      let endFlag = false;
      file.on('data', function (data) {
        file.pause();
        csvParse(data, function (e, d) {
          ....
          here is going some logic and
          ...
          fs.writeFileSync(f, data_)
          if (endFlag) {
              fs.closeSync(f);
          } else {
            file.resume();
          }
       })
    })
    file.on('end', function () {
        endFlag = true;
    })
  })

Upvotes: 1

Related Questions