2017-06-19 77 views
1

我想逐行讀取一個文件,並根據每行中的數據我必須對數據進行分類。Nodejs readline給出一些錯誤

var rd = readline.createInterface({ 
input: fs.createReadStream('/home/user/Desktop/text.txt'), 
output: process.stdout, 
console: false 
}); 

當執行該行時,將讀取並打印在終端中。

但是當我嘗試使用readline逐行讀取文件時,出現錯誤。

rd.on('line', (input) => { 
console.log(input); 
}); 

我收到以下錯誤。

`Interface { 
_sawReturnAt: 0, 
isCompletionEnabled: true, 
_sawKeyPress: false, 
_previousKey: 
{ sequence: '\n', 
name: 'enter', 
ctrl: false, 
meta: false, 
shift: false }, 
domain: 
Domain { 
domain: null, 
_events: { error: [Function: debugDomainError] }, 
_eventsCount: 1, 
_maxListeners: undefined, 
members: [] }, 
_events: { line: [ [Function], [Function], [Function] ] }, 
_eventsCount: 1, 
_maxListeners: undefined, 
output: 
    WriteStream { 
    connecting: false, 
    _hadError: false, 
    _handle: 
    TTY { 
    bytesRead: 0, 
    _externalStream: {}, 
    fd: 9, 
    writeQueueSize: 0, 
    owner: [Circular], 
    onread: [Function: onread] }, 
_parent: null, 
_host: null, 
_readableState: 
    ReadableState { 
    objectMode: false, 
    highWaterMark: 16384, 
    buffer: [Object], 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: null, 
    ended: false, 
    endEmitted: false, 
    reading: false, 
    sync: true, 
    needReadable: false, 
    emittedReadable: false, 
    readableListening: false, 
    resumeScheduled: false, 
    defaultEncoding: 'utf8', 
    ranOut: false, 
    awaitDrain: 0, 
    readingMore: false, 
    decoder: null, 
    encoding: null }, 
readable: false, 
domain: null, 
_events: 
    { end: [Object], 
    finish: [Function: onSocketFinish], 
    _socketEnd: [Function: onSocketEnd], 
    resize: [Object] }, 
_eventsCount: 4, 
_maxListeners: undefined, 
_writableState: 
    WritableState { 
    objectMode: false, 
    highWaterMark: 16384, 
    needDrain: false, 
    ending: false, 
    ended: false, 
    finished: false, 
    decodeStrings: false, 
    defaultEncoding: 'utf8', 
    length: 0, 
    writing: false, 
    corked: 0, 
    sync: false, 
    bufferProcessing: false, 
    onwrite: [Function: bound onwrite], 
    writecb: null, 
    writelen: 0, 
    bufferedRequest: null, 
    lastBufferedRequest: null, 
    pendingcb: 1, 
    prefinished: false, 
    errorEmitted: false, 
    bufferedRequestCount: 0, 
    corkedRequestsFree: [Object] }, 
writable: true, 
allowHalfOpen: false, 
destroyed: false, 
_bytesDispatched: 22151, 
_sockname: null, 
_writev: null, 
_pendingData: null, 
_pendingEncoding: '', 
server: null, 
_server: null, 
columns: 80, 
rows: 24, 
_type: 'tty', 
fd: 1, 
_isStdio: true, 
destroySoon: [Function], 
destroy: [Function] }, 
input: 
ReadStream { 
_readableState: 
    ReadableState { 
    objectMode: false, 
    highWaterMark: 65536, 
    buffer: [Object], 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: false, 
    ended: true, 
    endEmitted: true, 
    reading: false, 
    sync: false, 
    needReadable: false, 
    emittedReadable: false, 
    readableListening: false, 
    resumeScheduled: false, 
    defaultEncoding: 'utf8', 
    ranOut: false, 
    awaitDrain: 0, 
    readingMore: false, 
    decoder: null, 
    encoding: null }, 
    readable: false, 
    domain: 
    Domain { 
    domain: null, 
    _events: [Object], 
    _eventsCount: 1, 
    _maxListeners: undefined, 
    members: [] }, 
_events: { end: [Object], data: [Function: onData] }, 
_eventsCount: 2, 
_maxListeners: undefined, 
path: '/home/user/Desktop/text.txt', 
fd: null, 
flags: 'r', 
mode: 438, 
start: undefined, 
end: undefined, 
autoClose: true, 
pos: undefined, 
bytesRead: 31, 
destroyed: true, 
closed: true }, 
historySize: 30, 
crlfDelay: 100, 
_prompt: '> ', 
terminal: true, 
line: '', 
cursor: 0, 
history: [ '1,2,3', '4,8,4,8', '2,2,2,2', '1,2,3,4' ], 
historyIndex: -1, 
prevRows: 0, 
paused: true, 
closed: true }` 

請幫助我對此很陌生。

+0

你使用的是什麼版本的Node.js? 'node -v'。 – dan

+0

@dan我正在使用v7.7.2 –

+0

從[7.7.2文檔](https://nodejs.org/docs/v7.7.2/api/readline.html#readline_readline_createinterface_options)'console'不是一個選項'createInterface'正在等待,所以我認爲可以刪除。在你特定的例子中,你也可以刪除'output:process.stdout',因爲你在每行記錄日誌。從'createInterface'中刪除這兩個選項是否有幫助? – dan

回答

1

您的代碼看起來很好,但您使用的是Node.js REPL,而不是從文件運行代碼,這就是您看到此行爲的原因。

Node.js REPL(你在終端輸入node時得到的)將會存儲變量。但是,只有使用標識符時,該值也會返回。你看到的不是錯誤,而是rd對象。

Node.js REPL非常適合測試,但除非您有特定的使用原因,否則最好的辦法是創建一個app.js文件,將代碼添加到它,然後運行node app.js

如果您確實需要使用REPL編寫多行代碼,請務必在初始化時鍵入.editor,以便正確解釋空白等。

此外,從7.7.2文檔console不是createInterface期待的選項,以便可以刪除。在你的特例中,你也可以刪除output: process.stdout,因爲你的日誌記錄每行使用line事件

+0

完成'on'函數內部的過程後,如何記錄最終結果?我只是根據每行的長度創建一個'array'。最後,我想'記錄'最長行的長度 –

+0

您需要偵聽'close'事件來檢測整個文件何時被讀取。這裏有一個例子:https://stackoverflow.com/questions/43003920/the-changes-im-making-to-my-array-inside-linereader-on-are-not-available-outsid/43004107#43004107 – dan