如何从ES6中的解析器中获取xml2js结果?

Mik*_*ike 5 javascript fs node.js ecmascript-6 xml2js

我正在Node中构建一个服务器,它将搜索文件夹以查看是否存在XML文件(glob),如果存在,则将(fs)中的文件作为JSON对象(xml2js)读取并最终将其存储在数据库中某处.我想要将解析器的结果输出到另一个变量中,以便我可以对数据执行其他操作.从我所知道的情况来看,有些东西是同步运行的,但是我无法弄清楚如何阻止它并让我等到它完成继续前进.

我将我的功能分离到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...
          }
        });
      }
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

log*_*yth 9

await将等待一个Promise值来解决,否则它只会将它在promise中给出的值包装起来并立即解决这个问题.在你的例子中,

await glob('./filedrop/ALLREQUESTS-*.xml', (err, files) => {
Run Code Online (Sandbox Code Playgroud)

呼叫glob不返回Promise,所以await基本没用.所以你需要自己创造承诺.

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);
}
Run Code Online (Sandbox Code Playgroud)

请注意,现在此函数将拒绝它返回的promise,而不是强制记录错误.

你也可以用帮助器压缩它.例如,节点8包括util.promisify以使这样的代码更容易编写

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);
}
Run Code Online (Sandbox Code Playgroud)