简体   繁体   中英

ReferenceError: global is not defined in xml2js

I want to convert an XML String into JSON so as to fetch some user details. I am using xml2js library. Below is my typescript code:

typescript.ts

sendXML(){
    console.log("Inside sendXML method")
    console.log(this.XMLAsString)
    this.parseXML(this.XMLAsString);
}

parseXML(XMLAsString){
    var result;
    var parser=require('xml2js');
    parser.Parser().parseString(XMLAsString, (err, r)=>{result=r});
    console.log(result);
  }

ItrAgainstOrderFilTypeComponent.html

<button class="largeButton primaryButton" [disabled]="proceedToVerification" (click)="sendXML()">Proceed to Submit</button>

The sendXML() calls parseXML() function which parses the provided String XMLAsString . The parseXML() takes the String as a parameter and should print its equivalent JSON.

But when i try to run this code, i get the following error: ItrAgainstOrderFilTypeComponent.html:112 ERROR ReferenceError: global is not defined

This is the first time i am using xml2js lib.How do i resolve this? Also, why is it showing an error in the html file (Error is in the html provided above)?

It looks like you're trying to require a package inside your browser, it's usally the way to import packages in a Node.js script.

You may need to find another library or use Browserify .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM