nodejs异步在createReadStream中等待

编程入门 行业动态 更新时间:2024-10-09 22:19:55
本文介绍了nodejs异步在createReadStream中等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在逐行读取CSV文件并在MongoDB中插入/更新.预期的输出将是1. console.log(row);2. console.log(cursor);3.console.log("stream");

I am reading a CSV file line by line and inserting/updating in MongoDB. The expected output will be 1. console.log(row); 2. console.log(cursor); 3.console.log("stream");

但是得到类似的输出1. console.log(row);console.log(row);console.log(row);console.log(row);console.log(row);............ ............2. console.log(cursor);3.console.log("stream");请让我知道我在这里想念的东西.

But getting output like 1. console.log(row); console.log(row); console.log(row); console.log(row); console.log(row); ............ ............ 2. console.log(cursor); 3.console.log("stream"); Please let me know what i am missing here.

const csv = require('csv-parser'); const fs = require('fs'); var mongodb = require("mongodb"); var client = mongodb.MongoClient; var url = "mongodb://localhost:27017/"; var collection; client.connect(url,{ useUnifiedTopology: true }, function (err, client) { var db = client.db("UKCompanies"); collection = db.collection("company"); startRead(); }); var cursor={}; async function insertRec(row){ console.log(row); cursor = await collection.update({CompanyNumber:23}, row, {upsert: true}); if(cursor){ console.log(cursor); }else{ console.log('not exist') } console.log("stream"); } async function startRead() { fs.createReadStream('./data/inside/6.csv') .pipe(csv()) .on('data', async (row) => { await insertRec(row); }) .on('end', () => { console.log('CSV file successfully processed'); }); }

推荐答案

在您的 startRead()函数中, await insertRec()不会停止更多数据 insertRec()正在处理时,数据事件从流动开始.因此,如果您不希望在 insertRec()完成之前运行下一个 data 事件,则需要暂停然后恢复流.

In your startRead() function, the await insertRec() does not stop more data events from flowing while the insertRec() is processing. So, if you don't want the next data event to run until the insertRec() is done, you need to pause, then resume the stream.

async function startRead() { const stream = fs.createReadStream('./data/inside/6.csv') .pipe(csv()) .on('data', async (row) => { try { stream.pause(); await insertRec(row); } finally { stream.resume(); } }) .on('end', () => { console.log('CSV file successfully processed'); }); }

仅供参考,如果 insertRec()失败,您还需要一些错误处理.

FYI, you also need some error handling if insertRec() fails.

更多推荐

nodejs异步在createReadStream中等待

本文发布于:2023-11-23 23:13:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:nodejs   createReadStream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!