简体   繁体   English

ReferenceError:xml2js 中未定义全局

[英]ReferenceError: global is not defined in xml2js

I want to convert an XML String into JSON so as to fetch some user details.我想将 XML 字符串转换为 JSON 以获取一些用户详细信息。 I am using xml2js library.我正在使用 xml2js 库。 Below is my typescript code:下面是我的打字稿代码:

typescript.ts打字稿.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 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 . sendXML()调用解析提供的 String XMLAsString parseXML()函数。 The parseXML() takes the String as a parameter and should print its equivalent JSON. parseXML()将字符串作为参数,并应打印其等效的 JSON。

But when i try to run this code, i get the following error: ItrAgainstOrderFilTypeComponent.html:112 ERROR ReferenceError: global is not defined但是当我尝试运行此代码时,出现以下错误: ItrAgainstOrderFilTypeComponent.html:112 ERROR ReferenceError: global is not defined

This is the first time i am using xml2js lib.How do i resolve this?这是我第一次使用xml2js lib。我该如何解决这个问题? Also, why is it showing an error in the html file (Error is in the html provided above)?另外,为什么在 html 文件中显示错误(错误在上面提供的 html 中)?

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.看起来您正试图在浏览器中require一个包,这通常是在 Node.js 脚本中导入包的方式。

You may need to find another library or use Browserify .您可能需要找到另一个库或使用Browserify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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