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

use*_*171 5 mysql heroku node.js passport.js connect-flash

示例开始,我正在为节点应用程序进行用户注册,但是使用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) \
)');
Run Code Online (Sandbox Code Playgroud)

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

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

Error: ER_DUP_ENTRY: Duplicate entry 'xxxxxxxxxxxxxxxxx' for key 'facebook_id_UNIQUE'
Run Code Online (Sandbox Code Playgroud)

(我用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 }
Run Code Online (Sandbox Code Playgroud)

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

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

该代码在其他.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 
Run Code Online (Sandbox Code Playgroud)

该应用不会崩溃,用户仍然可以在浏览器中输入主页URL,并且一切正常。

知道出了什么问题吗?如何处理此错误,以便用户仅在配置文件页面上看到连接闪存消息?

小智 5

也许晚了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();
        }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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