【问题标题】:Node mongoose always returning error on findOne节点猫鼬总是在 findOne 上返回错误
【发布时间】:2015-08-28 08:07:43
【问题描述】:

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

说实话,我对节点很糟糕,但我想学习!

【问题讨论】:

  • 控制台打印的错误是什么?
  • 不幸的是,它不会向控制台打印任何内容,因为它是线程的(外部函数在日志之前完成),但是,通过使用 err.toString(),我可以猜到err === null 因为 err.toString() 抛出一个错误,说你不能在 null 上调用 toString() 。也许这是一个糟糕的假设。有什么建议吗?

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


【解决方案1】:
passport.use('local-signup',
 ...
    function(req, email, password, done) {

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

function( email, password, done) {

【讨论】:

  • 我改了,还是不行。顺便说一句,我知道我不是专家,但我有 4 个参数,因为我将 'passReqToCallback' 设置为 true,但我没有使用它,所以无论如何,摆脱它并进行更改是个好主意还有
  • 很抱歉,我之前从未使用过passReqToCallback
  • 不用担心,您知道为什么我的问题被否决了吗?我不确定我的问题有多糟糕
【解决方案2】:

嗯,我发现了问题。

听着孩子们,总是把你的 jimmy 包起来,总是把你的 if 语句关闭

【讨论】:

  • if (err) // 发生错误 console.log(err); err.toString(); return done(err);// 在这里?我的天……我也没见过。也许用 webstorm 之类的 ide 开发并经常格式化我们的代码是个好主意。
猜你喜欢
  • 1970-01-01
  • 2019-03-25
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多