英文:
can't read data in stream after sending it to a different method
问题
我正确地创建了一个可读流,但在上传该流后,流中的数据丢失了。
const readable: Readable = await createCsv(csv)
console.log('步骤 1', readable.read()) // <---- 数据存在
// 将 CSV 上传到 FTP 服务器
await this.ftp.uploadCSV(readable, pathway)
console.log('步骤 2', readable.read()) // <---- 数据不存在
// 返回 CSV 文件到前端
// 修复:不应该再次执行此操作
const r2: Readable = await createCsv(csv) // <--- 必须重新创建可读流以发送数据
return { readable: r2 }
如果this.ftp.uploadCSV()
返回流以便稍后在函数中返回,问题仍然相同。所以问题是如何将可读流发送到类的另一个方法,并在不必生成另一个流的情况下返回相同的可读数据,就像上面的代码一样。
英文:
I am creating a readable stream correctly, however, after uploading that stream it looses the data within the stream
const readable: Readable = await createCsv(csv)
console.log('step 1', readable.read()) // <---- the data is there
// uploadfile csv to FTP server
await this.ftp.uploadCSV(readable, pathway)
console.log('step 2', readable.read()) // <---- the data is not there
// return csv file to Front End
// FIXME: this shouldn't be done again
const r2: Readable = await createCsv(csv) // <--- have to create readable again to send back data
return { readable: r2 }
The issue is the same if this.ftp.uploadCSV()
returns back the stream to be returned later in the function. So the question is how can I send the readable stream to another method of a class and return that same readable data without having to generate another stream, like in the code above
答案1
得分: 1
uploadCSV
方法消耗了来自readable
流的数据,导致其变为空。为了避免这种情况,您应该克隆该流。关于流克隆的多个问题已经在Stack Overflow上提出,其中一个例子可以在这里找到。
英文:
uploadCSV
method consumes the data from the readable
stream, causing it to become empty. To avoid this, you should clone the stream. There are multiple questions about stream cloning on Stack Overflow, one example of which can be found here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论