Mongoose findOne护照策略中的回调永远不会返回

时间:2017-07-02 23:56:23

标签: node.js mongodb mongoose passport.js passport-local

我试图再次接受网页开发,并且我在护照策略中遇到问题,我打电话给User.findOne()检查名称或电子邮件是否已被使用。打印之前的console.log,但回调函数中的console.log没有。什么都没有崩溃。

当我启动服务器时,Mongoose报告成功连接到Mongodb(免费地图集沙箱)。

我怎样才能进一步解决这个问题?

    passport.use('local-signup', new LocalStrategy({
        usernameField : 'signup[email]',
        passwordField : 'signup[password]',
        passReqToCallback : true
    },
    function(req, email, password, done) {
        process.nextTick(function() {
        console.log('passport strategy local-signup: req.body.signup:', req.body.signup );
        User.findOne({$or: [
            { 'email': email },
            { 'name': req.body.signup.name }
            ]}, function(err, user) {
            console.log("FORTY TWO SV");
            if (err) return done(err);
            if (user && (user.name === req.body.signup.name)) {
                return done(null, false, { message: 'User name already taken.' });
            } else if (user && (user.email === email)) {
                return done(null, false, { message: 'An account with the email address already exists' });
            } else if (req.body.signup.password !== req.body.signup.verify) {
                return (done(null, false, { message: "The passwords don't match!"}));
            } else {
                var newUser = new User();
                newUser.email    = email;
                newUser.name     = req.body.signup.name;
                newUser.password = newUser.generateHash(password);
                console.log("new user being created!", newUser);
                return done(null, newUser);
                // newUser.save(function(err) {
                //     if (err) throw err;
                //     console.log('Created new user', newUser);
                //     return done(null, newUser);
                // });
            }
        });
    });
}));

我如何连接数据库:

// Connect to the database.
mongoose.connect(config.mongoUri, { useMongoClient: true });
mongoose.connection.on('error', console.error.bind(console, 'Connection error!'));
mongoose.connection.once('open', function(){
  console.log("MongoDB connected successfully");
});

如果我遗漏了可能有用的内容,那么完整的项目代码就在这里:https://github.com/brianbauer42/InfinitePlaylist/blob/master/server.js

任何提示都将受到高度赞赏,谢谢!

0 个答案:

没有答案