當我試圖運行我的代碼時,我得到了這個錯誤:
當我試圖運行我的代碼時,我得到了這個錯誤。
file://C:/Users/rb03/Documents/Testing/connect.js:27
for (const line of rl) {
^
TypeError: rl is not iterable
在file://C:/Users/rb03/Documents/Testing/connect.js:27:24。
在Connection.<anonymous>(C:Users.
b03檔案測驗
ode_modulesmysql2libconnection.js:777:13)
在Object。 onceWrapper (node: events: 514: 26)
在Connection。 emit (node: events:394:28)
在ClientHandshake.<anonymous> (C:Users)
b03檔案測驗
ode_modulesmysql2libconnection.js:121:14)
在ClientHandshake。 emit (node: events:394:28)
在ClientHandshake.execute (C:Users)
b03DocumentsTesting
ode_modulesmysql2libcommandscommand.js:44:10)
在Connection.handlePacket (C:Users)
b03DocumentsTesting
ode_modulesmysql2libconnection.js:456:32)
在PacketParser.onPacket (C:Users)
b03DocumentsTesting
ode_modulesmysql2libconnection.js:85:12)
在PacketParser.executeStart (C:Users)
b03DocumentsTesting
ode_modulesmysql2libpacket_parser.js:75:16)
我的代碼如下:
connection.connect(function(err) {
if (err) throw err;
console.log('Connected! ')。
const rl = readline.createInterface({ input: fs. createReadStream('./Logs/Outputs/split.lines.txt') })。)
let total = 0;
let buff = [];
for (const line of rl) {
buff.push([line])。
total 。
if (buff.length % 2000 == 0) {
connection.query("INSERT INTO test (line, timestamp, errortype) VALUES ?") 。
console.log(total)。
buff = [];
};
};
if (buff.length > 0) {
connection.query("INSERT INTO test (line, timestamp, errortype) VALUES ?") 。
console.log(total)。
};
connection.end()。
});
有誰知道該怎么做?Google上沒有顯示 "rl不是可迭代的",只有 "rl不是異步可迭代的"
。預先感謝!
uj5u.com熱心網友回復:
要進行迭代,需要:
for await (const line of rl)
而這需要在一個async函式中。 你缺少await。
rl有一個asyncIterator,但不是一個普通的迭代器,所以你需要await來使迭代作業。
如果你不想進行迭代,你可以使用常規的事件并監聽
line事件。
rl.on('line', line => /span> {
//在此處理行。
});
uj5u.com熱心網友回復:
如果你想為每一行輸入執行一段代碼,你可以使用line事件。
rl.on('line', (input) => //span> {
console.log(`Received: ${input}`)。)
});
參見Readline 檔案.
。編輯:有一個asyncIterator符號,可以用來迭代Readline.Interface。然而,它不是一個常規的for...of回圈,而是需要一個for await...of回圈。
for await (const line of rl) {
console.log(`Received: ${line}`)。)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/334227.html
標籤:
