search

Home  >  Q&A  >  body text

javascript - Mysql query exception occurs in node.js, how to deal with it?

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

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:37:53

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

    reply
    0
  • 某草草

    某草草2017-05-16 13:37:53

    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);
    });

    reply
    0
  • Cancelreply