捕获/处理MySQL重复输入错误 - 使用NodeJS,PassportJS,Express,connect-flash,Heroku

时间:2014-10-02 03:50:11

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

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

以下是创建表的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' s替换为实际的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消息?

2 个答案:

答案 0 :(得分:4)

也许它已经晚了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(错误)将错误打印到控制台时,我得到了它。

您可以在此检查所有错误编号https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html

很高兴!祝你读下这个好心灵的下一个好运。

答案 1 :(得分:1)

简单明了的解决方案: // ----------------

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