无法在发送到不同方法后从流中读取数据

huangapple go评论59阅读模式
英文:

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(&#39;step 1&#39;, readable.read()) // &lt;---- the data is there
// uploadfile csv to FTP server
await this.ftp.uploadCSV(readable, pathway)
console.log(&#39;step 2&#39;, readable.read()) // &lt;---- the data is not there

// return csv file to Front End
// FIXME: this shouldn&#39;t be done again
const r2: Readable = await createCsv(csv) // &lt;--- 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

huangapple
  • 本文由 发表于 2023年5月29日 15:06:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定