From 2e0cb7332c9a00962156f02d77fe283d2e089998 Mon Sep 17 00:00:00 2001 From: BL Date: Fri, 27 Apr 2018 12:43:50 +0530 Subject: [PATCH] parse CSV function debugging the delimiter error --- lib/AmazonMwsResource.js | 51 +++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/AmazonMwsResource.js b/lib/AmazonMwsResource.js index 01951c4..f6ccb75 100644 --- a/lib/AmazonMwsResource.js +++ b/lib/AmazonMwsResource.js @@ -151,41 +151,48 @@ AmazonMwsResource.prototype = { } } - function processResponseType(res, response, callback) { + function parseCSVFile(res, responseString, callback) { + var data = []; + csv.fromString(responseString, {headers: true, delimiter: '\t', ignoreEmpty: true}) + .on('data', function (value) { + data.push(value); + }) + .on('end', function () { + debug('response after parsing tab delimited file %o ', data); + var items = {}; + items.data = data; + items.Headers = { + 'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown', + 'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown', + 'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown', + 'x-mws-timestamp': res.headers['x-mws-timestamp'] + }; + return callback(null, items); + }) + .on('error', function (error) { + return callback(error); + }); + } + + function processResponseType(res, responseString, callback) { //debug('response %o ', response); //debug('res.headers %o ', res.headers); if (RESPONSE_CONTENT_TYPE.indexOf(res.headers['content-type'].toLowerCase()) > -1) { debug('It is XML Response'); - response = xml2json.toJson(response); + var items = xml2json.toJson(responseString); //debug('response after parsing JSON %o ', response); - response = JSON.parse(response); - response.Headers = { + items = JSON.parse(items); + items.Headers = { 'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown', 'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown', 'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown', 'x-mws-timestamp': res.headers['x-mws-timestamp'] }; //debug('after adding header response', response); - return callback(null, response); + return callback(null, items); } else { debug('It is NON-XML Response'); - var data = []; - csv.fromString(response, {headers: true, delimiter: '\t'}) - .on('data', function (value) { - data.push(value); - }) - .on('end', function () { - debug('response after parsing tab delimited file %o ', data); - response = {}; - response.data = data; - response.Headers = { - 'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown', - 'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown', - 'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown', - 'x-mws-timestamp': res.headers['x-mws-timestamp'] - }; - return callback(null, response); - }); + parseCSVFile(res, responseString, callback); } }