使用Passport(本地注册)创建用户注册功能,代码如下:
// config/passport.js
// load all the things we need
var LocalStrategy = require('passport-local').Strategy;
// load up the user model
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('./database');
var connection = mysql.createConnection(dbconfig.connection);
connection.query('USE ' + dbconfig.database);
// expose this function to our app using module.exports
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
connection.query("SELECT * FROM users WHERE id = ? ",[id], function(err, rows){
done(err, rows[0]);
});
});
passport.use(
'local-signup',
new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, password, 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)
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,
password: bcrypt.hashSync(password, null, null) // use the generateHash function in our user model
};
var insertQuery = "INSERT INTO users ( username, password ) values (?,?)";
connection.query(insertQuery,[newUserMysql.username, newUserMysql.password],function(err, rows) {
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
});
}
});
})
);
引发以下错误:
TypeError:无法读取属性' id'未定义的 在Query._callback(M:\ server \ config \ passport.js:70:55) 在Query.Sequence.end(M:\ server \ node_modules \ mysql \ lib \ protocol \ sequences \ Sequence.js:88:24) 在Query.ErrorPacket(M:\ server \ node_modules \ mysql \ lib \ protocol \ sequences \ Query.js:90:8) 在Protocol._parsePacket(M:\ server \ node_modules \ mysql \ lib \ protocol \ Protocol.js:279:23) 在Parser.write(M:\ server \ node_modules \ mysql \ lib \ protocol \ Parser.js:76:12) 在Protocol.write(M:\ server \ node_modules \ mysql \ lib \ protocol \ Protocol.js:39:16) 在Socket。 (M:\服务器\ node_modules \ MySQL的\ lib中\ Connection.js:103:28) 在emitOne(events.js:116:13) 在Socket.emit(events.js:211:7) 在addChunk(_stream_readable.js:263:12) 在readableAddChunk(_stream_readable.js:250:11) 在Socket.Readable.push(_stream_readable.js:208:10) 在TCP.onread(net.js:594:20)
使用console.log(rows);
返回undefined
。我该如何解决这个问题?
[编辑]
在回答被考虑后,产生了以下错误(因为rows.insertId
仍然未定义):
错误:无法将用户序列化为会话 传递(M:\ server \ node_modules \ passport \ lib \ authenticator.js:281:19) 在序列化(M:\ server \ node_modules \ passport \ lib \ authenticator.js:286:7) 在M:\ server \ config \ passport.js:24:9 在传递(M:\ server \ node_modules \ passport \ lib \ authenticator.js:294:9) 在Authenticator.serializeUser(M:\ server \ node_modules \ passport \ lib \ authenticator.js:299:5) 在SessionManager.logIn(M:\ server \ node_modules \ passport \ lib \ sessionmanager.js:14:8) 在IncomingMessage.req.login.req.logIn(M:\ server \ node_modules \ passport \ lib \ http \ request.js:50:33) 在Strategy.strategy.success(M:\ server \ node_modules \ passport \ lib \ middleware \ authenticate.js:248:13) 在验证时(M:\ server \ node_modules \ passport-local \ lib \ strategy.js:83:10)
答案 0 :(得分:2)
根据我的理解,这与MySQL
您共享的JSON没有id
var newUserMysql = {username:username,password:password};
然而,您正试图访问id
,这就是您获得undefined
newUserMysql.id = rows.insertId;
要解决此问题,您需要将id
添加到JSON。
var newUserMysql = {username: "username", password : "password", id:""};
答案 1 :(得分:0)
在整个时间里,答案都在我的鼻子底下。我所要做的就是改变这一点:
newUserMysql = rows.insertId;
进入这个:
newUserMysql = rows[0].insertId;
并且问题已成功解决。