vue.jsquasarxml2js

Having trouble with xml2js in Quasar 2.6.0


I need the following code to run in a Quasar 2.6.0 application.

async fetchAbstracts() {
  try {
    const url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=33888705,18340043,19033662,20978265,23204432,23275034,23892238&retmode=xml';
    const response = await axios.get(url);
    const xmlData = response.data;
    const parser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: true });

    parser.parseString(xmlData, (err, result) => {
      if (err) {
        throw err;
      }
      
      // Assuming the structure of the XML is known and consistent
      // Navigate to the abstract texts. This path might need adjustments
      const articles = result.PubmedArticleSet.PubmedArticle || [];
      articles.forEach(article => {
        const abstractText = article.MedlineCitation.Article.Abstract.AbstractText;
        
        // AbstractText can be an array if there are multiple paragraphs or sections
        if (Array.isArray(abstractText)) {
          abstractText.forEach((text, index) => {
            console.log(`Abstract Section ${index + 1}: ${text}`);
          });
        } else {
          console.log(`Abstract: ${abstractText}`);
        }
      });
    });
  } catch (error) {
    console.error('Error fetching or parsing the XML:', error);
  }
}

I have

import axios from "axios";
import xml2js from "xml2js";

In my script file but when I get to

 const parser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: true });

It throws an error of:

Error fetching or parsing the XML: TypeError: this.removeAllListeners is not a function

Solution

  • A solution comes from a GitHub issue for the same problem. Multiple people report the issue being resolved after installing the events package.

    npm install events
    

    or

    yarn add events