在循环中编写文件 - JS

时间:2018-02-07 10:30:19

标签: javascript node.js loops

当我尝试在循环中写入文件时,我似乎遇到了问题,即使第一个文件尚未创建,循环仍在迭代(我想我要么不理解承诺或脚本的异步性质)

所以在命令行上我将运行node write_file_script.js premier_league

// teams.js
module.exports = {
premier_league: [
  { team_name: "Team 1", friendly_name: "name 1"},
  { team_name: "Team 2", friendly_name: "name 2"}
 ]
}

我的剧本

const args = process.argv.slice(2);
const TEAM = require('./teams');

const Excel = require('exceljs');
const workbook = new Excel.Workbook();

for (var team = 0; team < TEAM[args].length; team++) {
  console.log("Starting Excel File generation for " + TEAM[args][team]['team_name']);
  var fhcw = require('../data_files/home/fhcw/' + TEAM[args][team]['friendly_name'] + '_home' + '.json');
  fhcw = fhcw.map(Number);

  workbook.xlsx.readFile('./excel_files/blank.xlsx')
  .then(function() {
    var worksheet = workbook.getWorksheet(1);
    // Write FHCW
    for (i=0; i < fhcw.length; i++) {
      col = i+6;
      worksheet.getCell('E'+ col).value = fhcw[i];
    }

    console.log(TEAM[args][team])
    workbook.xlsx.writeFile('./excel_files/' + TEAM[args] + '/' + TEAM[args][team]['friendly_name'] + '.xlsx');

  });

}

运行时得到的输出是

Starting Excel File generation for Team 1
Starting Excel File generation for Team 2
undefined
(node:75393) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): TypeError: Cannot read property 'friendly_name' of undefined

所以看起来文件没有被写入但是循环继续,我怎样才能确保在进入下一个循环之前写入文件?

由于

2 个答案:

答案 0 :(得分:1)

如果函数返回Promise,并且你想连续(一次一个)而不是并行(同时开始),你需要等待每一个你使用then()开始下一个。

另请注意,您的TEAM只是数组的导出(至少如所示),因此您无法提供args,这是其他错误的来源。< / p>

当你有一系列要做的事情时,最好的方法就是拥有一个你运行的队列,直到你的文件用完为止。在这种情况下,看起来您的TEAM数组是您的队列,但由于这是一个导出,我建议不一定要更改它,而是将其复制到另一个可以更改的数组:

const args = process.argv.slice(2);
const TEAM = require('./teams');

const Excel = require('exceljs');
const workbook = new Excel.Workbook();

const writeNextFile = (queue) => {
  // If we have nothing left in the queue, we're done.
  if (!queue.length) {
    return Promise.resolve();
  }

  const team = queue.shift(); // get the first element, take it out of the array

  console.log("Starting Excel File generation for " + team.team_name);
  var fhcw = require('../data_files/home/fhcw/' + team.friendly_name + '_home' + '.json');
  fhcw = fhcw.map(Number);

  // return this promise chain
  return workbook.xlsx.readFile('./excel_files/blank.xlsx')
  .then(function() {
    var worksheet = workbook.getWorksheet(1);
    // Write FHCW
    for (i=0; i < fhcw.length; i++) {
      col = i+6;
      worksheet.getCell('E'+ col).value = fhcw[i];
    }

    console.log(team);
    // not sure what you thought this would TEAM[args] would have translated to, but it wouldn't have been a string, so I put ??? for now
    // also, making the assumption that writeFile() returns a Promise.
    return workbook.xlsx.writeFile('./excel_files/' + team.??? + '/' + team.friendly_name + '.xlsx');
  }).then(() => writeNextFile(queue));
}

writeNextFile(TEAM.slice(0)) // make a copy of TEAM so we can alter it
  .then(() => console.log('Done'))
  .catch(err => console.error('There was an error', err)); 

基本上,我们的函数接受一个数组并写入第一个团队,然后递归调用自己来调用下一个团队。最终它会全部解决,你最终会得到一个最终解决的Promise。

说到Promises,你基本上总是需要将它们链接在一起。你将无法使用for循环或任何其他标准循环。

如果你想一次性写出它们,它会更加清晰,因为你可以为每个地图做一个非递归的地图,并使用Promise.all知道它们何时完成。

const writeNextFile = (team) => {
  console.log("Starting Excel File generation for " + team.team_name);
  var fhcw = require('../data_files/home/fhcw/' + team.friendly_name + '_home' + '.json');
  fhcw = fhcw.map(Number);

  // return this promise chain
  return workbook.xlsx.readFile('./excel_files/blank.xlsx')
  .then(function() {
    var worksheet = workbook.getWorksheet(1);
    // Write FHCW
    for (i=0; i < fhcw.length; i++) {
      col = i+6;
      worksheet.getCell('E'+ col).value = fhcw[i];
    }

    console.log(team);
    // not sure what you thought this would TEAM[args] would have translated to, but it wouldn't have been a string, so I put ??? for now
    // also, making the assumption that writeFile() returns a Promise.
    return workbook.xlsx.writeFile('./excel_files/' + team.??? + '/' + team.friendly_name + '.xlsx');
  });
}

Promise.all(TEAM.map(writeTeamFile))
  .then(() => console.log('Done')
  .catch(err => console.error('Error'));

Promise用于异步,通常当你有一组东西时,你想要并行执行它们,因为它更快。这就是为什么并行版本更清洁;我们不必递归地调用它。

答案 1 :(得分:1)

由于您没有写入同一文件,因此可以循环访问文件并执行读/写操作。

您可以在循环中绑定索引值,然后继续前进。

示例代码。

var filesWrittenCount = 0;

for(var team = 0; team < TEAM[args].length; team++){
    (function(t){
        // call your async function here. 
        workbook.xlsx.readFile('./excel_files/blank.xlsx').then(function() {
            var worksheet = workbook.getWorksheet(1);
            // Write FHCW
            for (i=0; i < fhcw.length; i++) {
                col = i+6;
                worksheet.getCell('E'+ col).value = fhcw[i];
            }

            console.log(TEAM[args][t])
            workbook.xlsx.writeFile('./excel_files/' + TEAM[args] + '/' + TEAM[args][t]['friendly_name'] + '.xlsx');

            // update number of files written.
            filesWrittenCount++;

            if (totalFiles == filesWrittenCount) {
                // final callback function - This states that all files are updated
                done();
            }

        });
    }(team));
}

function done() {
    console.log('All data has been loaded');
}

希望这会有所帮助。