javascript - Mysql query exception occurs in node.js, how to deal with it?
某草草
某草草 2017-05-16 13:35:53
0
2
746

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异常?
某草草
某草草

reply all(2)
我想大声告诉你

Process your sql and set illegal values ​​as default values

某草草

Try adding this processing to the end of the startup file
app.js

app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

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.

mongoose.connection.on('error', function (err) {
    console.error('Mongoose connection error: ' + err);
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!