【问题标题】:Catch/Handle MySQL Duplicate Entry Error - with NodeJS, PassportJS, Express, connect-flash, Heroku捕获/处理 MySQL 重复输入错误 - 使用 Node JS、Passport JS、Express、connect-flash、Heroku
【发布时间】:2014-11-27 01:17:27
【问题描述】:

我正在为节点应用程序进行用户注册,从 this 示例开始,但使用的是 MySQL 数据库而不是 MongoDB。 users 表中的多个列具有唯一性约束。

下面是创建表的 SQL 代码:

CREATE TABLE `' + dbconfig.database + '`.`' + dbconfig.users_table + '` ( \
    `id`                  INT UNSIGNED NOT NULL  AUTO_INCREMENT, \
    `username`            VARCHAR(60)  NULL, \
    `password`            CHAR(60)     NULL, \
    `facebook_id`         VARCHAR(60)  NULL, \
    `facebook_token`      CHAR(223)    NULL, \
    `facebook_name`       VARCHAR(100) NULL, \
    `facebook_email`      VARCHAR(100) NULL, \
    `google_id`           VARCHAR(60)  NULL, \
    `google_token`        CHAR(67)     NULL, \
    `google_name`         VARCHAR(100) NULL, \
    `google_email`        VARCHAR(100) NULL, \
    PRIMARY KEY (`id`), \
    UNIQUE INDEX `id_UNIQUE` (`id` ASC), \
    UNIQUE INDEX `username_UNIQUE` (`username` ASC), \
    UNIQUE INDEX `facebook_id_UNIQUE` (`facebook_id` ASC), \
    UNIQUE INDEX `facebook_token_UNIQUE` (`facebook_token` ASC), \
    UNIQUE INDEX `google_id_UNIQUE` (`google_id` ASC), \
    UNIQUE INDEX `google_token_UNIQUE` (`google_token` ASC) \
)');

当用户在他们的个人资料页面上并尝试更新其现有用户记录中的 facebook_id 时,更新可能会违反唯一性约束——即另一个用户记录已经具有该 facebook_id

Heroku 日志中反映的 MySQL 错误信息是:

Error: ER_DUP_ENTRY: Duplicate entry 'xxxxxxxxxxxxxxxxx' for key 'facebook_id_UNIQUE'

(我已经用 x 代替了实际的 facebook_id。)

除了返回用户已在的页面(他们的个人资料页面)并显示错误消息“Facebook ID 已注册给其他用户”之外,我什么也不想做。我正在尝试使用 connect-flash 模块来显示消息。这在程序的其他地方也有效。

我正在尝试按以下方式完成此操作:

if (err) {
    console.log("mylog : facebook connect error");
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
        //handle disconnect
    } else if (err.code === 'ER_DUP_ENTRY') {
        console.log("mylog : fb ER_DUP_ENTRY detected");
        // release connection created with pool.getConnection
        if (connection) connection.release();
        // req.flash to set flashdata using connect-flash
        return done(err, false, req.flash('loginMessage', 'Facebook ID registered to another user.')); 
    } else { //continue }

在 profile.ejs 页面上,我有以下内容:

<% if (message.length > 0) { %>
    <div class="alert alert-danger"><%= message %></div>
<% } %>

此代码正在其他 .ejs 页面上运行。

在 ER_DUP_ENTRY 错误的情况下,上面显示的两个 console.log 语句都在注册,因此程序成功检测到 ER_DUP_ENTRY 错误,但不是像我希望的那样返回到 profile.ejs 页面,配置文件页面消失了并且并且在浏览器中显示了很多文字,开头是:

Error: ER_DUP_ENTRY: Duplicate entry 'xxxxxxxxxxxxxxxxx' for key 'facebook_id_UNIQUE' at Query.Sequence._packetToError 

应用程序没有崩溃,用户仍然可以在浏览器中输入主页 URL,一切正常。

知道出了什么问题吗?如何处理此错误,以便用户仅在个人资料页面上看到 connect-flash 消息?

【问题讨论】:

    标签: mysql node.js heroku passport.js connect-flash


    【解决方案1】:

    也许它迟到了 3 年,但我要留下答案,迟到总比没有好!。

       if(err){  //we make sure theres an error (error obj)
    
            if(err.errno==1062){   
            req.flash('message','The entry already exist.'); //we send the flash msg
            return res.redirect('/admin/userPanel');
            db.end();
            }
            else{
                throw err;
            db.end();
            }
    }
    

    errorno 是您要查找的属性,我在使用 console.log(err) 将 err 打印到控制台时得到它。

    这里可以查看所有错误号https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html

    很高兴!祝下一个阅读本文的好人好运。

    【讨论】:

      【解决方案2】:

      非常直截了当的解决方案: //----------------

      connection.query('write your mysql query', function(err,rows){
            if(err)
            {
      
              if(err.code == 'ER_DUP_ENTRY' || err.errno == 1062)
              {
                  console.log('Here you can handle duplication')
              }
              else{
                 console.log('Other error in the query')
              }
      
            }else{
               console.log('No error in the query')
            }
       })
      

      【讨论】:

        猜你喜欢
        • 2012-05-24
        • 2016-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多