节点mongoose总是在findOne上返回错误

我使用passport.js来login用户,每当我的本地身份validationfunction到达User.findOne(),它总是返回一个错误。 我不知道我在做什么错…

我的护照代码与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}); }); 

我真的很害怕节点,但我想学习!

 passport.use('local-signup', ... function(req, email, password, done) { 

函数期望三个参数email, password,done
改变callback函数

 function( email, password, done) { 

那么,我发现了这个问题。

在这里听着孩子们,总是把你的吉米包裹起来,并且总是closures你的陈述