Harsha W
Harsha W

Reputation: 3366

Using java script functions in node js

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.

Upvotes: 0

Views: 74

Answers (1)

ArUn
ArUn

Reputation: 1337

XML is undefined so define XML

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

Upvotes: 0

Related Questions