gpt4 book ai didi

node.js - 在 exceljs 包中的 worksheet.eachRow() 中使用 wait 调用异步函数

转载 作者:行者123 更新时间:2023-12-03 08:03:59 27 4
gpt4 key购买 nike

const Excel = require("exceljs");
const fs = require("fs");
const createConnection = require("./db.connection");
const SEGMENT = require("./models/segment");

const fileName = "test.xlsx";

createConnection(); //Db connection

const readExcel = async () => {
try {
let workbook = new Excel.Workbook();

await workbook.xlsx.readFile(fileName);

readRows(workbook);
} catch (error) {
console.log("error", error);
}
};

readExcel();

readRows = (workbook, headers) => {
workbook.eachSheet((worksheet, sheetId) => {
// console.log(worksheet);

worksheet.eachRow({ includeEmpty: false }, async (row, rowNumber) => {
console.log("Entered ROW at LOC 116", rowNumber);
const segment = new SEGMENT({ versionName: row.getCell(1).text.trim() });
const saveSegment = await segment.save(); //Insert into db
console.log("Entered ROW at LOC 119 Save Segment", saveSegment); //Further code after this line
});
});
};

我只想等到文档保存在数据库中,然后再在 workbook.eachRow 循环中进行进一步处理。我得到的输出是

Entered ROW at LOC 116 1
Entered ROW at LOC 116 2
Entered ROW at LOC 116 3
Entered ROW at LOC 116 4
Entered ROW at LOC 116 5
Entered ROW at LOC 116 6
Entered ROW at LOC 116 7
Entered ROW at LOC 116 8
Entered ROW at LOC 116 9
Entered ROW at LOC 116 10
Entered ROW at LOC 116 11
Entered ROW at LOC 116 12
Entered ROW at LOC 116 13
Entered ROW at LOC 116 14
Entered ROW at LOC 116 15
Entered ROW at LOC 116 16
Entered ROW at LOC 116 17
Entered ROW at LOC 116 18
Entered ROW at LOC 116 19
Entered ROW at LOC 116 20
Entered ROW at LOC 116 21
Entered ROW at LOC 116 22
Entered ROW at LOC 116 23
Entered ROW at LOC 116 24
Entered ROW at LOC 116 25
Entered ROW at LOC 116 26
Entered ROW at LOC 116 27
Entered ROW at LOC 116 28
Entered ROW at LOC 116 29
Entered ROW at LOC 116 30
Entered ROW at LOC 116 31
Entered ROW at LOC 116 32
Entered ROW at LOC 116 33
Entered ROW at LOC 116 34
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f92,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f93,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}

这意味着每一行都会被执行,直到我在循环内调用await之前。虽然我想要的是每行的整个处理应该在移动到第二行之前完成,因为 workbook.eachrow 是一个同步函数。基本输出应该是这样的。

Entered ROW at LOC 116 1
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f92,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}
Entered ROW at LOC 116 2
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f93,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}

这与我们在 forEach 循环中使用 async/await 时遇到的问题相同吗?有解决方法吗?

最佳答案

实现它的一种方法是使用 for 循环:

for (let i = 1; i <= worksheet.rowCount; i++) {
const row = worksheet.getRow(i)
}

另一种方法可以是用 promise 包装 eachRow 调用:

await new Promise((resolve) => {
worksheet.eachRow(async (row, i) => {
const result = await someAsyncFn()

if (i === worksheet.rowCount) {
resolve()
}
})
}

关于node.js - 在 exceljs 包中的 worksheet.eachRow() 中使用 wait 调用异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73043352/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com