Based on the express framework, node-mysql module development project needs to execute sql statements in the project. If the sql statement is abnormal, the program will crash directly.
conn.query(sql,function (err, result) {}
如果这里的事情了因为前端提交方式的原因(或者是别人的非法提交),sql语句变为了
SELECT * FROM prod WHERE prodName LIKE'%undefined%' LIMIT NaN,10;
这样的话,node控制台会抛出:throw err; // Rethrow non-MySQL errors
node就崩溃了,请问有什么好的方式处理sql异常?
Process your sql and set illegal values as default values
Try adding this processing to the end of the startup file
app.js
Then you need to check the API of mysql connector. Generally, it must have an exception handling mechanism.
I use mongoose. Add exception handling when declaring the connection.