节点猫鼬总是在 findOne 上返回错误
Node mongoose always returning error on findOne
我正在使用 passport.js 登录用户,每当我的本地身份验证函数到达 User.findOne() 时,它总是 returns 并出现错误。我不知道我做错了什么...
我调用 findOne() 的护照代码:
passport.serializeUser(function(user, done) {
console.log('serialize user occurred');
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local-signup',
new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password'
},
function(email, password, done) {
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
// 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
User.findOne({ 'local.email' : email }, function(err, user) {
if (err) // An error occurred
console.log(err);
err.toString();
return done(err);
if (user) { // This email is already in use
return done(null, false);
} else { // Valid email to sign in wth
var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
})
);
我的用户模型:
var userSchema = mongoose.Schema({
local : {
email : String,
password : String
},
facebook : {
id : String,
token : String,
email : String,
name : String
}
});
// methods ==============
// Generate a hash for a password
userSchema.methods.generateHash = function(password){
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// Checking if password is valid
userSchema.methods.comparePassword = function(password){
return bcrypt.compareSync(password, this.local.password);
};
module.exports = mongoose.model('User', userSchema);
还有我的路线,如果你感兴趣的话:
app.get('/signupfail', function(req, res) {
// render the page and pass in any flash data if it exists
res.json({message: "failed to sign in, failure redirect"});
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signupfail', // redirect back to the signup page if there is an error
falureFlash : true // allow flash messages
}));
app.get('/profile', isLoggedIn, function(req, res) {
var user = req.user // This is the user extracted from the session
res.json({message: "hooray it worked for: "+user.local.email});
});
老实说,我对 node 很糟糕,但我想学习!
passport.use('local-signup',
...
function(req, email, password, done) {
该函数需要三个参数 email, password,done
。
将回调函数更改为
function( email, password, done) {
嗯,我找到问题了。
听着孩子们,永远把你的吉米包起来,永远关闭你的 if 语句
我正在使用 passport.js 登录用户,每当我的本地身份验证函数到达 User.findOne() 时,它总是 returns 并出现错误。我不知道我做错了什么...
我调用 findOne() 的护照代码:
passport.serializeUser(function(user, done) {
console.log('serialize user occurred');
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local-signup',
new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password'
},
function(email, password, done) {
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
// 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
User.findOne({ 'local.email' : email }, function(err, user) {
if (err) // An error occurred
console.log(err);
err.toString();
return done(err);
if (user) { // This email is already in use
return done(null, false);
} else { // Valid email to sign in wth
var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
})
);
我的用户模型:
var userSchema = mongoose.Schema({
local : {
email : String,
password : String
},
facebook : {
id : String,
token : String,
email : String,
name : String
}
});
// methods ==============
// Generate a hash for a password
userSchema.methods.generateHash = function(password){
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// Checking if password is valid
userSchema.methods.comparePassword = function(password){
return bcrypt.compareSync(password, this.local.password);
};
module.exports = mongoose.model('User', userSchema);
还有我的路线,如果你感兴趣的话:
app.get('/signupfail', function(req, res) {
// render the page and pass in any flash data if it exists
res.json({message: "failed to sign in, failure redirect"});
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signupfail', // redirect back to the signup page if there is an error
falureFlash : true // allow flash messages
}));
app.get('/profile', isLoggedIn, function(req, res) {
var user = req.user // This is the user extracted from the session
res.json({message: "hooray it worked for: "+user.local.email});
});
老实说,我对 node 很糟糕,但我想学习!
passport.use('local-signup',
...
function(req, email, password, done) {
该函数需要三个参数 email, password,done
。
将回调函数更改为
function( email, password, done) {
嗯,我找到问题了。
听着孩子们,永远把你的吉米包起来,永远关闭你的 if 语句