Node mongoose总是在findOne上返回错误

时间:2015-06-12 10:35:11

标签: javascript node.js mongodb mongoose passport.js

我使用passport.js登录用户,每当我的本地身份验证功能到达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});
});

我对节点很诚实,但我想学习!

2 个答案:

答案 0 :(得分:1)

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

该函数需要三个参数email, password,done 将回调函数更改为

function( email, password, done) {

答案 1 :(得分:0)

好吧,我发现了问题。

在这里听孩子们,总是包裹你的吉米,并且总是关闭你的if语句