用户注册时出错(Express.Js、Passport、MySQL)

Posted

技术标签:

【中文标题】用户注册时出错(Express.Js、Passport、MySQL)【英文标题】:Error during user registration (Express.Js, Passport, MySQL) 【发布时间】:2018-04-13 11:26:26 【问题描述】:

我正在使用 Expressjs、Passport(local) 和 mysql 处理 Registration 页面,但我的代码似乎无法正常运行。

这是我的查询(MySQL):

connection.query('\CREATE TABLE `' + dbconfig.database + '`.`' + dbconfig.users_table + '` ( \
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, \
`useremail` VARCHAR(20) NOT NULL, \
`password` CHAR(60) NOT NULL, \
`passwordHint` VARCHAR(5) NOT NULL, \
`hintanswer` VARCHAR(60) NOT NULL, \
    PRIMARY KEY (`id`), \
UNIQUE INDEX `id_UNIQUE` (`id` ASC), \
UNIQUE INDEX `useremail_UNIQUE` (`useremail` ASC) \)');

这是护照策略:

passport.use(
        'local-signup',
        new LocalStrategy(
            usernameField : 'useremail',
            passwordField : 'password',
            passwordHintField:'passwordHint',//change
            hintanswerField:'hintanswer',//change
            passReqToCallback : true 
        ,
        function(req, useremail, password,passwordHint,hintanswer,done) 
             connection.query("SELECT * FROM users WHERE useremail = ?",[useremail], function(err, rows) 
                if (err)
                    return done(err);
                if (rows.length) 
                    return done(null, false, req.flash('signupMessage', 'That useremail is already taken.'));
                 
                else 
                     var newUserMysql = 
                        useremail: useremail,
                        password: bcrypt.hashSync(password, null, null),  // use the generateHash function in our user model
                        passwordHint:bcrypt.hashSync(passwordHint, null, null),//change
                        hintanswer:bcrypt.hashSync(hintanswer, null, null)//change
                    ;
                    var insertQuery = "INSERT INTO users ( useremail, password ,passwordHint,hintanswer) values (?,?,?,?)";//change
                    connection.query(insertQuery,[newUserMysql.useremail, newUserMysql.password,newUserMysql.passwordHint,newUserMysql.hintanswer],function(err, rows) //change
                        newUserMysql.id = rows.insertId;
                        return done(null, newUserMysql);
                    );
                
            );
        )
    );

这是我得到的错误:

TypeError:无法读取未定义的属性“insertId” 在 Query._callback (C:\Users\tpdle\dev\Ttareungyi-Navi\passport\passport.js:72:47) 在 Query.Sequence.end (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24) 在 Query.ErrorPacket (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\sequences\Query.js:90:8) 在 Protocol._parsePacket (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\Protocol.js:279:23) 在 Parser.write (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\Parser.js:76:12) 在 Protocol.write (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\Protocol.js:39:16) 在套接字。 (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\Connection.js:103:28) 在 emitOne (events.js:96:13) 在 Socket.emit (events.js:188:7) 在 readableAddChunk (_stream_readable.js:176:18)

`

【问题讨论】:

【参考方案1】:

如果您仍然遇到此问题,请替换:

var insertQuery = "INSERT INTO users ( useremail, password ,passwordHint,hintanswer) values (?,?,?,?)";
connection.query(insertQuery,[newUserMysql.useremail, newUserMysql.password,newUserMysql.passwordHint,newUserMysql.hintanswer],function(err, rows) 
     newUserMysql.id = rows.insertId;
     return done(null, newUserMysql);
);

用这个:

var insertQuery = "INSERT INTO users SET ?";
connection.query(insertQuery, newUserMysql, function(err, rows) 
     if(err) 
       console.log(err);
       return done(null, err);
     else
       newUserMysql.id = rows.insertId;
       return done(null, newUserMysql);
     
);

它为我完成了这项工作。

【讨论】:

以上是关于用户注册时出错(Express.Js、Passport、MySQL)的主要内容,如果未能解决你的问题,请参考以下文章

如何在 express.js 中创建注册路由?

Express.js 对 Angular CORS 错误的响应

在 Express.js 中刷新 JWT

React 前端 + Express.js 会话

Express.js 和 Bluebird - 处理承诺链

phonegap ajax 用户身份验证与 nodejs-express-mongodb-passport js