簡體   English   中英

節點貓鼬總是在findOne上返回錯誤

[英]Node mongoose always returning error on findOne

我正在使用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});
});

我真的很討厭node,但是我想學習!

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

該函數需要三個參數email, password,done
將回調函數更改為

function( email, password, done) {

好吧,我發現了問題。

在這里聽孩子們,總是包裹着你的吉米,並且總是關閉你的if語句

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM