簡體   English   中英

readline.createInterface 不會填充到 NodeJs 中的數組

[英]readline.createInterface does not populate to array in NodeJs

我對 NodeJs 和 Javascript 比較陌生。 請原諒我的瑣碎問題。 我正在嘗試使用以下代碼讀取文件,並且我想逐行讀取並填充到數組中。 我可以打印,但無法返回包含所有行的數組。 我想使用 NodeJs 非阻塞 api 來讀取文件。 請幫助我了解以下代碼中的問題。

public readFileLineByLineNonBlocking(fileNamePath: string): any {
        let allLines: string[] = [];

        const readLines = readline.createInterface({
            input: fs.createReadStream(fileNamePath),
            output: process.stdout,
            terminal: false
        });

        readLines.on('line', (line) => {
            allLines.push(line);
//            console.log(line); // Prints the line
        });

        // The following lines do not print
        allLines.forEach((line) => {
            console.log("IP Address: ", line);
        });

        return allLines;
    }

為了測試,我寫了以下 class。 如果您運行以下 class,您應該會在控制台中看到要打印的每一行。

從“fs”導入*作為fs; 從“readline”導入readline;

class Test1 {

    public readFileLineByLineNonBlocking(fileNamePath: string): String[] {
        let allLines: string[] = [];

        const readLines = readline.createInterface({
            input: fs.createReadStream(fileNamePath),
            output: process.stdout,
            terminal: false
        });

        readLines.on('line', (line) => {
            allLines.push(line);
        }).on('close', () => {
            // allLines is fully populated here
            // this is where you can use the value
            allLines.forEach((line) => {
                // console.log("IP Address: ", line); // I do not want to print
            });
        }).on('error', err => {
            console.log(err);
        });

        return allLines;
    }

}

let test1 = new Test1();
const fileNamePath: string = "testData/ipaddress-50.txt";

let allLines: any = test1.readFileLineByLineNonBlocking(fileNamePath);
console.log("All Lines: ", allLines);// Coming as empty

readLines.on('line', ...)正在注冊一個事件處理程序,該處理程序將在未來多次調用,而不是立即調用。 而且,它不會阻止等待所有行完成。 它只是注冊事件處理程序並立即繼續。

因此,您的代碼在填充之前嘗試使用allLines

您需要為close事件注冊一個事件處理程序:

public readFileLineByLineNonBlocking(fileNamePath: string): any {
        let allLines: string[] = [];

        const readLines = readline.createInterface({
            input: fs.createReadStream(fileNamePath),
            output: process.stdout,
            terminal: false
        });

        readLines.on('line', (line) => {
            allLines.push(line);
        }).on('close', () => {
            // allLines is fully populated here
            // this is where you can use the value
            allLines.forEach((line) => {
                console.log("IP Address: ", line);
            });
        });
    }

而且,整個操作是異步的,所以你不能直接返回結果。 您需要在close事件處理程序中使用它,從那里調用回調並將結果傳遞給它,將整個事情包裝在 promise 或使用不同的機制來獲取行,例如異步迭代器。


僅供參考,當我在一個文件中單獨運行這個確切的代碼時,它工作得很好:

const fs = require('fs');
const readline = require('readline');

function readFileLineByLineNonBlocking(fileNamePath) {
        let allLines = [];

        const readLines = readline.createInterface({
            input: fs.createReadStream(fileNamePath),
            output: process.stdout,
            terminal: false
        });

        readLines.on('line', (line) => {
            allLines.push(line);
        }).on('close', () => {
            // allLines is fully populated here
            // this is where you can use the value
            allLines.forEach((line) => {
                console.log("IP Address: ", line);
            });
        }).on('error', err => {
            console.log(err);
        });
}

readFileLineByLineNonBlocking("file1.txt");

所以,(回應你的評論說這不起作用)導致你的錯誤的任何原因要么是因為你的代碼看起來不像這樣,要么問題是由其他東西引起的,而不是由這個代碼引起的。

暫無
暫無

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

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