try {
db_conn.query(sqlq, (error, results, fields) => {
if (error) throw error;
if (!results.length) {
throw new Error('df');
}
return;
});
} catch (e) {
res.status(400)
res.send()
return 0;
}
我在第5行中抛出错误以退出sql查询功能,并且我希望try-catch能够捕获该错误并将状态设置为400并发送响应。这就是结果。
D:\CodingData\SID-API-Server\node_modules\mysql\lib\protocol\Parser.js:80
throw err; // Rethrow non-MySQL errors
^
Error: df
at Query.db_conn.query (D:\CodingData\SID-API-Server\routes\api.js:45:15)
at Query.<anonymous> (D:\CodingData\SID-API-Server\node_modules\mysql\lib\Connection.js:502:10)
at Query._callback (D:\CodingData\SID-API-Server\node_modules\mysql\lib\Connection.js:468:16)
at Query.Sequence.end (D:\CodingData\SID-API-Server\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (D:\CodingData\SID-API-Server\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
at Query.EofPacket (D:\CodingData\SID-API-Server\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
at Protocol._parsePacket (D:\CodingData\SID-API-Server\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (D:\CodingData\SID-API-Server\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (D:\CodingData\SID-API-Server\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\CodingData\SID-API-Server\node_modules\mysql\lib\Connection.js:91:28)
可能nodejs mysql软件包捕获错误,无所事事。几天前,此代码有效。 sql查询没有错误。