bhushankummar / amazon-mws

Amazon MWS NodeJS Wrapper
MIT License
197 stars 78 forks source link

Fix csv parsing errors #122

Closed jakeleventhal closed 4 years ago

jakeleventhal commented 4 years ago

When downloading a report, the csv is not getting parsed properly for one of my users. I ran an investigation and found that the MWS API is returning the data with a double newline towards the end, which is causing the issue.

To catch the issue, I downloaded the report as a txt file using the amazon-mws module with the following snippet in AmazonMwsResource.js:

        function parseCSVFile(res, responseString, delimiter, callback) {
            // Beginning of new code i added
            const fs = require('fs');
            fs.writeFile('/Users/jakeleventhal/Downloads/report.txt', responseString, (err) => {
                if (err) throw err;
            });
            // End of new code i added
            ...

I then created test.js that juse copies over the parseCSVFile from the AmazonMwsResource.js file so that I can try to correct the behavior:

var csv = require('fast-csv');

function parseCSVFile(res, responseString, delimiter) {
  var data = [];

  var options = {
    delimiter: delimiter,
    headers: true,
    discardUnmappedColumns: true,
    quote: null,
    ignoreEmpty: true,
    trim: true
  };

  /* BAD CODE
  if (responseString.includes('\n\n')) {
    responseString = responseString.split('\n\n')[1];
  }
  */

  // GOOD CODE
  responseString = responseString.replace(/\n\n/g, '');

  csv.fromString(responseString, options)
    .on('data', function (value) {
      data.push(value);
    })
    .on('end', function () {
      var items = {};
      items.data = data;
      console.log('bruh', items);
      return items;
    })
    .on('error', function (error) {
      debug('error ', error);
      return error;
    });
}

var fs = require('fs');
var text = fs.readFileSync('/Users/jakeleventhal/Downloads/report.txt').toString();

parseCSVFile(null, text, '\t')

I replaced the "BAD CODE" with "GOOD CODE" and was able to get the result I am looking for. The problem was that the \n\n was appearing in the middle of my report. By replacing this with the regex replace, the report now parses correctly.

bhushankummar commented 4 years ago

@jakeleventhal It has been fixed in https://github.com/bhushankumarl/amazon-mws/releases/tag/v0.0.26.