【问题标题】:Mongoose findOne not completing猫鼬 findOne 未完成
【发布时间】:2014-08-12 23:21:19
【问题描述】:

在尝试调试自己并搜索谷歌后,我的猫鼬 findOne 没有完成!它到达了线路并且没有结束,直到大约 2 分钟后超时。以下是调用顺序的代码

----------------路由

// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
    successRedirect : '/profile', // redirect to the secure profile section
    failureRedirect : '/', // redirect back to the home page if there is an error
    failureFlash : true // allow flash messages
}));

------------ 护照处理

passport.use('local-signup', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField    : 'email',
        passwordField    : 'password',
        passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
    },
    function (req, email, password, done) {

        // asynchronous
        process.nextTick(function () {
            //  Whether we're signing up or connecting an account, we'll need
            //  to know if the email address is in use.
            UserController.findUserByEmail(email, function (existingUser) {
                console.log(existingUser);
                // check to see if there's already a user with that email
                if (existingUser)
                    return done(null, false, req.flash('signupMessage', 'That email is already taken.'));

                //  If we're logged in, we're connecting a new local account.
                if (req.user) {
                    var user = req.user;
                    user.email = email;
                    user.password = encrypt.encrypt(password);
                    user.save(function (err) {
                        if (err)
                            throw err;
                        return done(null, user);
                    });
                }
                //  We're not logged in, so we're creating a brand new user.
                else {
                    console.log("there");
                    // create the user
                    User.createUser(req.body.username, email, req.body.username, password, "0", 1, function (result) {
                        return done(null, result);
                    });
                }

            });
        });

    }));

-----------------创建的数据客户端(用户控制器)

exports.findUserByEmail = function(email, callback){
User.findOne({email : email}, function(err, result){
    if(err){
        throw err;
    }else{
        callback(result);
    }
});
};

-----------------用户架构

//user Schema
var userSchema = mongoose.Schema({
name : String,
email  : String,
userName : String,
password : String,
IP : String,
//type of user. It can be a user admin 0, user student 1, user presentor (professor) 2, (user     guest 3) ?.
type : Number
});

//instance of my schema
var User = mongoose.model('User', userSchema);

代码在 User.findOne() 之前运行;然后就挂了。添加一些调试语句表明该方法之前的所有内容都被调用,但它从未进入回调。它只是在等待。

其他模式的其他查询运行良好,并且返回速度非常快。

我为 mongoose 添加了一个错误事件侦听器,但没有任何反应。

我的快递服务器也只在连接打开后运行,因此排除了这种情况。

您能提供的任何帮助将不胜感激!

-斯蒂芬

【问题讨论】:

  • 你在某个地方给mongoose.connect打电话吗?
  • 在启动服务器时是。 “我的快递服务器也只在连接打开后才运行,因此排除了这种情况。”
  • 来吧!我知道你能做到!
  • 你能用一个最小的例子重现这个问题吗?你在这里有很多事情要做。
  • 这是重现此“错误”的代码和路径。带有护照模块的 Express 应用程序。然后把它放进去。任何少的东西都违背了我在这里发帖的目的。我可以在自运行脚本中完全访问和查询 mongo。正是这条通过我的应用程序的路线停滞了

标签: node.js mongodb express mongoose


【解决方案1】:

我遇到了同样的问题。我的问题是我使用的是mongoose.createConnection

let connection = mongoose.createConnection(url, ...)

然后尝试使用猫鼬创建模型

let Tank = mongoose.model('Tank', yourSchema);

当您使用 createConnection 时,您需要使用该连接 - https://mongoosejs.com/docs/models.html#constructing-documents。

let Tank = connection.model('Tank', yourSchema);

【讨论】:

    【解决方案2】:

    我已经解决了这个问题:

    控制器文件是父目录中另一个节点项目的一部分。我假设节点没有默认读取当前节点进程加载的模块,而是尝试读取当时未运行的其他节点项目。只需将文件移动到同一节点项目文件夹即可解决问题。

    【讨论】:

    • 看来我的建议“在您尝试使用它们之前确保您的模型是必需的”确实是您正在寻找的答案。
    【解决方案3】:

    好的,首先,我认为您过于复杂了。先看一下mongoose statics:http://mongoosejs.com/docs/guide.html#statics

    现在,为什么需要那个单独的 UserController? (并且由于这不是 MVC 控制器,因此您的命名约定令人困惑——如果您打算将 User 和 UserController 分开,请考虑重命名)。考虑一下,你为什么需要那种方法呢?我的意思是,为什么不直接从您的中间件函数调用 User.findOne({email: "some email address"})?

    无论您选择哪种风格...

    我认为您的问题是您在尝试使用 UserController 之前没有获得对 User 模型的引用。

    在调用 UserController 中的 User.findOne 之前添加这一行:

    var User = mongoose.model('User');
    

    看看这是否有效。 (在尝试使用它们之前,请确保您的模型是必需的)。

    【讨论】:

    • 至于命名约定,目标是让我的学生(编写它的)提供更高级别的 api。对于简单的查找,当然没有必要,但是当聚合等发挥作用时,我们为其他要完成其他任务的学生提供了一个简单的 api 到文档集合中。不过还是谢谢你的建议!编辑添加静态看起来像一个更好的约定!也感谢您的链接!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2023-03-26
    • 2012-11-07
    • 2015-04-05
    • 2019-03-25
    • 2019-09-10
    • 2019-02-13
    相关资源
    最近更新 更多