最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get xml2js results out of the parser in ES6? - Stack Overflow

programmeradmin4浏览0评论

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);

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);
Share Improve this question asked Jul 26, 2017 at 17:03 MikeMike 1171 gold badge1 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

await will wait for a Promise value to resolve, otherwise it'll just wrap the value it is given in a promise and resolve the promise right away. In your example,

await glob('./filedrop/ALLREQUESTS-*.xml', (err, files) => {

the call to glob does not return a Promise, so the await is essentially useless. So you need to create the promise yourself.

exports.checkFileDrop = async () => {
  console.log('Checking for xml in filedrop...');

  const files = await new Promise((resolve, reject) => glob('./filedrop/ALLREQUESTS-*.xml', (err, files) => {
    if (err) reject(err);
    else resolve(files);
  });

  const parser = new xml2js.Parser();

  const data = await new Promise((resolve, reject) => fs.readFile('./filedrop/ALLREQUESTS-20170707.xml', 'utf16le', function (err, data) {
    if (err) reject(err);
    else resolve(data);
  });

  const result = await new Promise((resolve, reject) => parser.parseString(data, (err, result) => {
    if (err) reject(err);
    else resolve(result);
  });

  console.log('data found');

  const requests = JSON.stringify(result.Records.Record);

  console.log(requests);
}

Note that now this function will reject the promise it returns instead of force-logging the error.

You can also condense this down with a helper. Node 8 for instance includes util.promisify to make code like this easier to write, e.g.

const util = require('util');

exports.checkFileDrop = async () => {
  console.log('Checking for xml in filedrop...');

  const files = await util.promisify(glob)('./filedrop/ALLREQUESTS-*.xml');
  const parser = new xml2js.Parser();

  const data = await util.promisify(fs.readFile)('./filedrop/ALLREQUESTS-20170707.xml', 'utf16le');

  const result = await util.promisify(parser.parseString.bind(parser))(data);

  console.log('data found');

  const requests = JSON.stringify(result.Records.Record);

  console.log(requests);
}

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)
  }
}
发布评论

评论列表(0)

  1. 暂无评论