【问题标题】:how to find out the exact name of error thrown in mysql node.js如何找出mysql node.js中抛出的错误的确切名称
【发布时间】:2013-08-23 08:48:54
【问题描述】:

我正在用 node.js (express) 和 mysql felix 构建一个服务器。

我的问题是有时我的查询会返回重复错误。

我已经尝试了所有方法来尝试捕获这个错误,但没有运气,程序崩溃了。

这是我的代码:

 this.addReservation = function(req, res, next) {
      var json = JSON.parse(req.body['reservation']);
      var post = {user1: json.player1, user2: json.player2, courtId: json.id, hour: json.hour, date: json.date};
      var giveback = {u1_first: req.user.firstName, u1_last: req.user.lastName, user1: json.player1, user2: json.player2};

      connection.query('insert into ordering set ?', post, function(err, result){
          if (err){ 
                throw err;
                return res.send(500);
            }
            else 
                return res.json({res: giveback})
                //return res.send(200);            
      });   
  };

现在我已经尝试用 try and catch 包装这个查询。没有帮助。

我尝试输入此行:

connection.on('error', function() {console.log();}); 也没有帮助。

我试着把这条线放在其他地方仍然没有帮助。

我的控制台中抛出的错误是:Error: ER_DUP_ENTRY

我做错了什么?我应该在与“错误”不同的东西上使用“开”吗?

唯一对我有帮助的是:

process.on('uncaughtException', function(err) {
    // handle the error safely
    console.log(err);
});

但这非常糟糕,因为我知道导致的错误并且我希望处理它,所以这对我来说是一个非常糟糕的解决方案。

请帮助我,谢谢。我正在使用 mysql felix 库。

【问题讨论】:

  • 检查github.com/felixge/node-mysql#error-handlingconnection.query('USE name_of_db_that_does_not_exist', function(err, rows) { console.log(err.code); // 'ER_BAD_DB_ERROR' });
  • 我已经阅读了这篇文章,但我仍然不明白为什么我的程序崩溃了

标签: javascript mysql node.js express


【解决方案1】:
this.addReservation = function(req, res, next) {
  var json = JSON.parse(req.body['reservation']);
  var post = {user1: json.player1, user2: json.player2, courtId: json.id, hour: json.hour, date: json.date};
  var giveback = {u1_first: req.user.firstName, u1_last: req.user.lastName, user1: json.player1, user2: json.player2};

  connection.query('insert into ordering set ?', post, function(err, result){
      if (err){ 

            console.log('exact name of error thrown '+err);
            return res.send(500);
        }
        else 
            return res.json({res: giveback})
            //return res.send(200);            
  });   
};

【讨论】:

    猜你喜欢
    • 2019-01-03
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 2020-12-18
    • 2013-01-22
    • 2017-03-29
    • 2017-09-23
    • 1970-01-01
    相关资源
    最近更新 更多