mysql 不工作的本地护照

passport-local with mysql not working

我正在使用 node.js 和护照以及 mysql 作为用户登录。 我的主要来源是https://github.com/manjeshpv/node-express-passport-mysql/issues

我想在 table 中添加更多列。我从 emailfield 开始并更改如下代码。我只是在我认为需要的地方添加了电子邮件变量。我找不到崩溃的错误。无需修改任何内容,代码确实有效。

passport.js:

passport.use(
    'local-signup',
    new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'username',
        passwordField : 'password',
        //emailField : 'email',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, username, password, email, done) {
        // find a user whose email is the same as the forms email
        // we are checking to see if the user trying to login already exists
        connection.query("SELECT * FROM users WHERE username = ?",[username], function(err, rows) {
            if (err)
                log.info(err);
                //return done(err);
            if (rows.length) {
                return done(null, false, req.flash('signupMessage', 'That username is already taken.'));
            } else {
                // if there is no user with that username
                // create the user
                var newUserMysql = {
                    username: username,
                    email: email,
                    password: bcrypt.hashSync(password, null, null)  // use the generateHash function in our user model

                };

                var insertQuery = "INSERT INTO users ( username, password, email ) values (?,?,?)";
                connection.query(insertQuery,[newUserMysql.username, newUserMysql.password, newUserMysql.email],function(err, rows) {
                    newUserMysql.id = rows.insertId;

                    return done(null, newUserMysql);
                });
            }
        });
    })
);

这里是日志:

The magic happens on port 8080
GET /signup 200 20ms - 1.21kb
D:\node-express-passport-mysql\node_modules\mysql\lib\protocol\Parser.js:82
        throw err;
              ^
TypeError: undefined is not a function
    at Object.SqlString.escape (D:\node-express-passport-mysql\node_modules\mysq
l\lib\protocol\SqlString.js:46:13)
    at D:\node-express-passport-mysql\node_modules\mysql\lib\protocol\SqlString.
js:80:19
    at String.replace (native)
    at Object.SqlString.format (D:\node-express-passport-mysql\node_modules\mysq
l\lib\protocol\SqlString.js:71:14)
    at Connection.format (D:\node-express-passport-mysql\node_modules\mysql\lib\
Connection.js:263:20)
    at Connection.query (D:\node-express-passport-mysql\node_modules\mysql\lib\C
onnection.js:196:22)
    at Query._callback (D:\node-express-passport-mysql\config\passport.js:71:32)

    at Query.Sequence.end (D:\node-express-passport-mysql\node_modules\mysql\lib
\protocol\sequences\Sequence.js:96:24)
    at Query._handleFinalResultPacket (D:\node-express-passport-mysql\node_modul
es\mysql\lib\protocol\sequences\Query.js:144:8)
    at Query.EofPacket (D:\node-express-passport-mysql\node_modules\mysql\lib\pr
otocol\sequences\Query.js:128:8)
28 Jun 21:03:58 - [nodemon] app crashed - waiting for file changes before starti
ng...

这看起来是问题所在:

function(req, username, password, email, done) {

您添加了一个不应该存在的额外参数 email。由于它破坏了 done 回调,当您的代码尝试调用它时,它将导致 "undefined is not a function" 错误。

如果您要传递额外的 email 属性,您可以通过 req.body.email 访问它(假设您使用 POST 路由登录).