Non
Non

Reputation: 8589

Nodejs on xml to json error: TypeError: Cannot read property 'toString' of null

I am trying to convert some XML to json

I have here this into a function

var parseString = require('xml2js').parseString

   _.map(dataset, function(items) {
     return _.map(items, function(item) {
       cancel = parseString(item.XX_CANCEL, function(item) {return item;});
       dropdown = parseString(item.XX_VIEW, function(item) {return item;});
       fillOpen = parseString(item['XX_FILL OPEN'], function(item) {return item;});
       return item;
     });
   });

when I try to execute it, I get this error

/home/mretana/Documents/Projects/capilleiraclickandgamblebackend/node_modules/mssql/node_modules/tedious/node_modules/babel-runtime/regenerator/runtime.js:493
        throw exception;
              ^
TypeError: Cannot read property 'toString' of null
    at Parser.exports.Parser.Parser.parseString ...

just for you to know, for example if I remove the parseString(...) and do

cancel = item.XX_CANCEL;

console.log(cancel)
logs ---> <element><element_type>BASIC_CHECKBOX</element_type><element_call/><element_content>1</element_content></element>

it logs that XML element, that is why I am doing it with the parseString,

cancel = parseString(item.XX_CANCEL, function(item) {return item;});

so, what do you think is going on ?

All I need is to convert those XML elements into JSON.

Upvotes: 1

Views: 2249

Answers (1)

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

Looking at the API of xml2js, it is evident that parseString accepts a callback using the standard form of function(err, res), whereas you are using the form function(item).

Upvotes: 1

Related Questions