5

我正在为节点应用程序进行用户注册,从这个示例开始,但使用 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 消息?

4

2 回答 2

7

也许迟到了 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

很高兴!祝下一个读到这篇文章的好人好运。

于 2018-04-28T02:20:55.230 回答
3

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

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')
      }
 })
于 2018-09-18T13:30:32.573 回答