簡體   English   中英

讀取 stream (nodejs) 時,“無法 pipe。響應已經發出”

[英]"Failed to pipe. The response has been emitted already" when reading a stream (nodejs)

所以我的代碼應該從 CSV 文件中讀取一些行,將它們轉換為 JSON 對象數組,然后返回該數組。 要將文件讀取為 stream,我使用got ,然后在fast-csv中使用它。 為了返回結果數組,我將整個內容放入 Promise 中,如下所示:

async GetPage() : Promise<{OutputArray:any[], StartingIndex:number}>{
        return new Promise(async (resolve, reject) => {
            const output:any[] = []; 
            const startingIndex = this.currentLocation;
            try{
                parseStream(this.source, {headers:true, maxRows:this.maxArrayLength, skipRows:this.currentLocation, ignoreEmpty:true, delimiter:this.delimiter})
                    .on('error', error => console.log(`parseStream: ${error}`))
                    .on('data', row => {
                        const obj = this.unflatten(row); // data is flattened JSON, need to unflatten it
                        output.push(obj); // append to output array
                        this.currentLocation++;
                    })
                    .on('end', (rowCount: number) => {
                        console.log(`Parsed ${this.currentLocation} rows`);
                        resolve({OutputArray:output, StartingIndex:startingIndex});
                    });
            }
            catch(ex){
                console.log(`parseStream: ${ex}`);
                throw new Error(ex);
            }
        })
    }

現在,當我調用它一次( await GetPage() )時,它工作得非常好。 問題是當我連續第二次調用它時。 我得到以下信息:

UnhandledPromiseRejectionWarning: Error: Failed to pipe. The response has been emitted already.

我在這里看到了一個類似的案例: https://github.com/sindresorhus/file-type/issues/342但據我所知,這是一個不同的案例,或者更確切地說,如果它是相同的,我不知道如何在此處應用解決方案。

GetPage是 class CSVStreamParser中的一個方法,它在構造函數中被賦予一個Readable ,我像這樣創建 Readable: readable:Readable = got.stream(url)

讓我困惑的是,我的第一個版本的 GetPage 沒有包含 Promise,而是接受了一個回調(我只是發送了console.log來測試它),當我連續調用它幾次時沒有錯誤,但它可以不返回值,所以我將其轉換為 Promise。

謝謝: :)

首先,刪除兩個async ,因為您已經返回Promise

然后刪除try/catch塊並throw ,因為您不應該拋出 promise。 而是使用reject function。

GetPage() : Promise<{OutputArray:any[], StartingIndex:number}>{
    return new Promise((resolve, reject) => {
        const output:any[] = []; 
        const startingIndex = this.currentLocation;
        parseStream(this.source, {headers:true, maxRows:this.maxArrayLength, skipRows:this.currentLocation, ignoreEmpty:true, delimiter:this.delimiter})
            .on('error', error => reject(error))
            .on('data', row => {
                const obj = this.unflatten(row); // data is flattened JSON, need to unflatten it
                output.push(obj); // append to output array
                this.currentLocation++;
            })
            .on('end', (rowCount: number) => {
                console.log(`Parsed ${this.currentLocation} rows`);
                resolve({OutputArray:output, StartingIndex:startingIndex});
            });
    });
}

這里有一些資源可以幫助您了解異步函數承諾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM