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