[英]NodeJs csv-parse await inside on('data')
I have a code, which reades .csv line by line and saves each parsed row to the database我有一个代码,它逐行读取 .csv 并将每个解析的行保存到数据库中
const csv = require('csv-parse')
const errors = []
csv.parse(content, {})
.on('data', async function (row: any) {
const error = await tryToSaveToDatabase(row);
if (error) {
errors.push(error)
}
})
.on('end', function () {
// somehow process all errors
})
but, unfortunately, .on('end', ...
block is beeing called earlier then all await
block succeded.但是,不幸的是, .on('end', ...
块被调用得更早,然后所有await
块都成功了。
I have read NodeJs Csv parser async operations - seems we cannot use await
inside .on('data', ...
callback.我已经阅读了 NodeJs Csv 解析器异步操作- 似乎我们不能在.on('data', ...
回调中使用await
。
What is the correct way to perform such thing - if I want to read .csv line by line (files might be very huge - so it must be performed in a streaming manner) and collect some errors while saving to database?执行此类操作的正确方法是什么 - 如果我想逐行读取 .csv (文件可能非常大 - 因此必须以流式方式执行)并在保存到数据库时收集一些错误? (these errors are displayed on frontend then) (这些错误会显示在前端)
https://csv.js.org/parse/api/async_iterator/此解决方案逐行读取 .csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.