How to get xml2js results out of the parser in ES6?

Viewed 12228

I'm building a server in Node that will search a folder to see if an XML file exists (glob), and if it does, read the file in (fs) as a JSON object (xml2js) and eventually store it in a database somewhere. I'm want to get the results OUT of the parser and into another variable so I can do other things with the data. From what I can tell, something is running synchronously, but I can't figure out how to stop it and for me to wait until it's finished to continue moving on.

I'm separating my function out into a controller elsewhere from app.js:

app.controller.js

const fs = require('fs-extra');
const glob = require('glob');
const xml2js = require('xml2js');

exports.requests = {};

exports.checkFileDrop = async () => {
  console.log('Checking for xml in filedrop...');
  // this is the only place await works...
  await glob('./filedrop/ALLREQUESTS-*.xml', (err, files) => {
    var parser = new xml2js.Parser();
    // this is looking for a specific file now, which I'll address later once I can figure out this issue
    fs.readFile('./filedrop/ALLREQUESTS-20170707.xml', 'utf16le', function (err, data) { 
      if (err) {
        console.log('ERROR: ', err);
      } else {
        parser.parseString(data, (err, result) => {
          if (err) {
            console.log('ERROR: ', err);
          } else {
            console.log('data found');
            exports.requests = JSON.stringify(result.Records.Record);
            // data is outputted here correctly
            console.log(exports.requests);
            // this doesn't even seem to want to save to exports.requests anyways...
          }
        });
      }
    });
  });
}

app.js

const appController = require('./controllers/app.controller');

// check if there is file in filedrop
appController.checkFileDrop();
// prints out an empty object
console.log(appController.requests);
// can't do anything if it doesn't exist yet
appController.saveToDB(appController.requests);
2 Answers

You can use async/await

import fs from 'fs';
import { promisify } from 'util';

const xmlToJson = async filePath => {
  const parser = new xml2js.Parser
  try {
    const data = await fs.promises.readFile(filePath, 'utf8')
    const result = await promisify(parser.parseString)(data);
    const requests = JSON.stringify(result.merchandiser.product);
    return requests
  }
  catch(err) {
    console.log(err)
  }
}
Related