[英]code: 'ERR_HTTP_HEADERS_SENT' } error - uncaughtException: Error [ERR_STREAM_WRITE_AFTER_END]: write after end
[英]mssql - uncaughtException in Stream Mode
我面臨與此處提到的相同的問題:
嘗試使用流和查詢表而不提及schema
。
類似於select * from table
而不是select * from schema_name.table
。 我收到以下錯誤: mssql uncaughtException RequestError: Invalid object name
我能夠在聽readStream.on('error')
時記錄錯誤,但它仍然拋出uncaughtException
。
軟件版本
"mssql": "^7.2.1",
"nodejs": "12.15.0"
預期行為:
應該能夠捕捉到 uncaughtException
實際行為:
INFO: waaaaaa1
uncaughtException RequestError: Invalid object name 'employee'.
at handleError (.../node_modules/mssql/lib/tedious/request.js:388:15)
at Connection.emit (events.js:223:5)
at Connection.emit (.../node_modules/tedious/lib/connection.js:1071:18)
at Parser. (.../node_modules/tedious/lib/connection.js:1176:12)
at Parser.emit (events.js:223:5)
at Readable. (.../node_modules/tedious/lib/token/token-stream-parser.js:27:14)
配置:
import mssql from 'mssql';
try {
mssql.connect({
server,
port: Number.parseInt(port),
database,
user,
password,
options: {
encrypt: true, // for azure
trustServerCertificate: trustServerCertificate ? true : false // change to true for local dev / self-signed certs
}
})
.then(async client => {
const readStream = client.request();
readStream.stream = true;
const passThroughStream = new stream.PassThrough();
const writeStream = fs.createWriteStream(path.join(__dirname, './writeMe.json'));
readStream.on('error', () => log.info('waaaaaa1'));
passThroughStream.on('error', () => log.info('waaaaaa2'));
writeStream.on('error', () => log.info('waaaaaa3'));
readStream.pipe(passThroughStream).pipe(writeStream);
readStream.query(query);
})
// the promise error handler
.catch(() => {
log.info('waaaaaa4');
});
// the main sql error handler:
mssql.on('error', error => {
log.error('SQL error1', error);
});
// the main sql error handler:
client.on('error', error => {
log.error('SQL error2', error);
});
}
catch (err) {
log.info('catchhhh meee', err);
}
finally {
if(client){
client.close();
}
}
嘗試添加一個 catch 處理程序來connect
並且對文檔中提到的mssql
對象更重要
mssql.connect({
// ...
})
.then(() => {
return readStream.query(query);
})
// the promise error handler
.catch(() => {
})
// the main sql error handler:
mssql.on('error', error => {
console.error('SQL error')
})
最后使用const readableStream = request.toReadableStream();
解決了這個問題const readableStream = request.toReadableStream();
完整代碼:
const request = client.request();
const readableStream = request.toReadableStream();
const writeStream = fs.createWriteStream(path.join(__dirname, './writeMe.json'));
const passThroughStream = new stream.PassThrough();
readableStream.on('end', () => passThroughStream.end());
readableStream.on('error', (e) => log.info('waaaaaa1'));
readableStream.pipe(passThroughStream).pipe(writeStream);
request.query(query);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.