2015-06-28 115 views
0

我使用node.js和护照和MySQL的userlogin。 我的主要来源是从https://github.com/manjeshpv/node-express-passport-mysql/issues护照本地与mysql不工作

我想在表中添加更多的列。我开始使用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... 

回答

1

这看起来是问题:

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

您添加一个额外的参数email该不该”不要在那里。由于它会破坏done回调,因此当您的代码尝试调用它时,会导致“未定义不是函数”错误。

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