Cannot read property 'Parser' of undefined promise error

1.2k views Asked by At

I was parsing some data via API my code for the parsing is given below,

  parseXML(data) {
    return new Promise((resolve) => {
      var k: string | number,
        arr = [],
        parser = new xml2js.Parser({
          trim: true,
          explicitArray: true,
        });
      parser.parseString(data, function (err, result) {
        var obj = result.ApiResponse;
        for (k in obj.CommandResponse) {
          var item = obj.CommandResponse[k];
          arr.push({
            info: item.DomainCheckResult[0],
          });
        }
        resolve(arr);
      });
    });
  }

This is my parser code but still i'm getting console error with
core.js:4197 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'Parser' of undefined TypeError: Cannot read property 'Parser' of undefined

So my question was what's wrong with the code? as I was returning a promise what extra could I do? I'm returning a new promise and after that I have called the parser, so how could I get error? Can anyone help me?

1

There are 1 answers

0
MD Nasirul Islam On

Well, there should be a ',' instead of the ';'. After tweaking for a long time I was finally able to solve the problem.

  parseXML(data) {
    return new Promise((resolve) => {
      var k: string | number,
        arr = [],
        parser = new xml2js.Parser({
          trim: true,
          explicitArray: true,
        });
      // var parser = new xml2js.Parser({ explicitArray: false });
      parser.parseString(data, function (err, result) {
        var obj = result.ApiResponse;
        for (k in obj.CommandResponse) {
          var item = obj.CommandResponse[k];
          arr.push({
            Name: item.DomainCheckResult[0]['$']['Domain'],
            Available: item.DomainCheckResult[0]['$']['Available'],
            PremiumRegistrationPrice:
              item.DomainCheckResult[0]['$']['PremiumRegistrationPrice'],
          });
        }     console.log(JSON.stringify(item.DomainCheckResult));
      });
    });
  }

BTW thank you everyone who tried to help. Ohh and another thing on the top I also did an error on importing the xml2js I also had to change that.

import xml2js from 'xml2js';