Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 猫鼬没有完成_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js 猫鼬没有完成

Node.js 猫鼬没有完成,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,在尝试调试自己并搜索谷歌后,我的猫鼬findOne没有完成!它到达终点线,直到2分钟后超时才结束。下面是调用它的顺序代码 ----------------路由 // process the signup form app.post('/signup', passport.authenticate('local-signup', { successRedirect : '/profile', // redirect to the secure profile section fai

在尝试调试自己并搜索谷歌后,我的猫鼬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);
                    });
                }

            });
        });

    }));
------------------已创建数据客户端(UserController)

------------------用户模式

//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添加了一个错误事件监听器,但什么也没有出现

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

如果您能提供任何帮助,我们将不胜感激


-斯蒂芬

好的,首先,我认为你把这件事复杂化了。首先看一下猫鼬静态:

现在,为什么需要单独的用户控制器?(并且鉴于这不是MVC控制器,您的命名约定很混乱——如果您打算将用户和用户控制器分开,请考虑重命名)。想想,你为什么需要那个方法?我的意思是,为什么不直接从中间件函数调用User.findOne({email:“某些电子邮件地址”})

不管你选择什么风格

我认为您的问题在于,在尝试使用UserController之前,您没有在UserController中获得对用户模型的引用

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

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

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

好的,首先,我认为您的操作过于复杂了。首先看一下猫鼬静态:

现在,为什么需要单独的用户控制器?(并且鉴于这不是MVC控制器,您的命名约定很混乱——如果您打算将用户和用户控制器分开,请考虑重命名)。想想,你为什么需要那个方法?我的意思是,为什么不直接从中间件函数调用User.findOne({email:“某些电子邮件地址”})

不管你选择什么风格

我认为您的问题在于,在尝试使用UserController之前,您没有在UserController中获得对用户模型的引用

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

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

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

我已经解决了这个问题:


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

我已解决此问题:


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

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

let connection = mongoose.createConnection(url, ...)
然后尝试使用猫鼬来创建模型

let Tank = mongoose.model('Tank', yourSchema);
使用createConnection时,需要使用该连接-


我也面临同样的问题。我的问题是我使用的是
mongoose.createConnection

let connection = mongoose.createConnection(url, ...)
然后尝试使用猫鼬来创建模型

let Tank = mongoose.model('Tank', yourSchema);
使用createConnection时,需要使用该连接-


您是否调用
mongoose.connect
到某个地方?启动服务器时是。“我的express服务器也只在连接打开时运行,因此排除了这种情况。”快点!我知道你能做到!你能用一个简单的例子再现这个问题吗?这里有很多内容。这是代码,也是复制这个“bug”的路径。带有passport模块的Express应用程序。那就把这个放进去。再少一点都不符合我在这里发帖的目的。我可以在一个自动运行的脚本中完全访问和查询mongo。正是通过我的应用程序的这条路径暂停了。您是否调用
mongoose.connect
某个地方?启动服务器时是的。“我的express服务器也只在连接打开时运行,因此排除了这种情况。”快点!我知道你能做到!你能用一个简单的例子再现这个问题吗?这里有很多内容。这是代码,也是复制这个“bug”的路径。带有passport模块的Express应用程序。那就把这个放进去。再少一点都不符合我在这里发帖的目的。我可以在一个自动运行的脚本中完全访问和查询mongo。正是通过我的应用程序的这条路线阻碍了命名约定,目标是让我的学生(编写它的学生)提供更高级别的api。对于一个简单的发现,当然这不是必需的,但是当聚合等开始发挥作用时,我们为其他要完成其他任务的学生提供了一个简单的api到文档集合中。谢谢你的建议!编辑添加静态看起来是一个更好的约定!谢谢你的链接!至于命名约定,目标是让我的学生(编写它的学生)提供更高级别的api。对于一个简单的发现,当然这不是必需的,但是当聚合等开始发挥作用时,我们为其他要完成其他任务的学生提供了一个简单的api到文档集合中。谢谢你的建议!编辑添加静态看起来是一个更好的约定!谢谢你的链接!似乎我的建议“在你尝试使用之前,确保你的模型是必需的”确实是你想要的答案