1
votes

I'm working on a Registration page using Expressjs, Passport(local) and MySQL but my code doesn't seem to function properly.

This is my query (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) \)');

This is the passport strategy:

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

And this is the error I get:

TypeError: Cannot read property 'insertId' of undefined at Query._callback (C:\Users\tpdle\dev\Ttareungyi-Navi\passport\passport.js:72:47) at Query.Sequence.end (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24) at Query.ErrorPacket (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\sequences\Query.js:90:8) at Protocol._parsePacket (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\Protocol.js:279:23) at Parser.write (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\Parser.js:76:12) at Protocol.write (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\protocol\Protocol.js:39:16) at Socket. (C:\Users\tpdle\dev\Ttareungyi-Navi\node_modules\mysql\lib\Connection.js:103:28) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18)

`

1

1 Answers

0
votes

If you 're still having this issue, replace this:

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

with this:

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

It did the job for me.