0

I am using the below JS library in order to convert a JSON to XML in NodeJS.

XML.ObjTree

I created a JS file called XMLJSONParser.js and added the XML.ObjTree content there as below.

module.exports = function () { XML.ObjTree = function () { return this; }; ................ More code };

In the controller, I have the below code in order to do the conversion.

const XMLs = require('../common/XMLJSONParser');
router.post('/', async (req, res) => {
    try {
       var { tasks } = req.body;
        var xotree = new XMLs.XML.ObjTree();
        var tree1 = {tasks}
        var xml1 = xotree.writeXML( tree1 );
        alert( "xml1: "+xml1 );
     }

When calling I am getting the exception

message:"Cannot read property 'ObjTree' of undefined" stack:"TypeError: Cannot read property

Am I using the correct way when calling the JS file from Node JS?

I was able to run and get the output in https://js.do properly.

Harsha W
  • 3,162
  • 5
  • 43
  • 77
  • The `exports` you posted as the first snippet is somehow incomplete, however, it kind of looks like you are exporting **a function**. But then, when you import it, you expect it to be an object with `.XML` property. If this is so, the function surely doesn't have the property so that you are not allowed to further ask for `.ObjTree()`. – Wiktor Zychla Oct 03 '19 at 09:25
  • Remove `module.exports = function () {` and add at the bottom of your XMLJSONParser.js file this `module.exports = XML;` the you can do `const XML = require('../common/XMLJSONParser'); XML.ObjTree();` – Molda Oct 03 '19 at 09:35

1 Answers1

0

XML is undefined so define XML

module.exports = function () {
 let XML = {};
 XML.ObjTree = function () {
        return this;
    };
................ More code
};
ArUn
  • 1,317
  • 2
  • 23
  • 39