json2xls icon indicating copy to clipboard operation
json2xls copied to clipboard

When parsing an array of objects, it fails if one object is null

Open captixn opened this issue 4 years ago • 0 comments

Hi,

When parsing an array of object with: var xls = json2xls(json) I stumbled onto this error :

        if (n in object) {
              ^

TypeError: Cannot use 'in' operator to search for 'count' in null
    at getByString (C:\Users\capti\dev\essec_scraper\node_modules\[4mjson2xls[24m\lib\json2xls.js:33:15)
    at C:\Users\capti\dev\essec_scraper\node_modules\[4mjson2xls[24m\lib\json2xls.js:70:25
    at Array.map (<anonymous>)
    at C:\Users\capti\dev\essec_scraper\node_modules\[4mjson2xls[24m\lib\json2xls.js:69:23
    at Array.map (<anonymous>)
    at Function.transform.prepareJson (C:\Users\capti\dev\essec_scraper\node_modules\[4mjson2xls[24m\lib\json2xls.js:68:24)
    at transform (C:\Users\capti\dev\essec_scraper\node_modules\[4mjson2xls[24m\lib\json2xls.js:4:26)
    at file:///C:/Users/capti/dev/essec_scraper/convertData.js:7:11
[90m    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)[39m
[90m    at async Loader.import (internal/modules/esm/loader.js:166:24)[39m

It appears function getByString(object, path) does not check for null for "object". Therefore,

transform.prepareJson = function(json,config) {
....
res.rows = jsonArr.map(function(row) {
        return fields.map(function(key) {
            var value = getByString(row,key);


Will fail. As a simple fix, I added

    if(object == null){
        return ""
    } 

to function getByString(object, path). So that the rest of the array can be prepared..

captixn avatar Feb 20 '21 18:02 captixn