2014-12-06 57 views
3

我已经定义了这个路由,但是对它的任何请求都被卡在'pending'并且永远运行。Mongoose .find()方法导致请求挂起

当我登录的代码,我看到1其次是4,这意味着find方法中的代码永远不会被执行

# Calendar routes 
    router.get '/calendars', (req, res) -> 
    console.log '1' 
    Calendar.find (err, calendars) -> 
     console.log "2" + err 
     console.log "3" + calendars 
     res.send(err) if err 
     res.json(calendars) 
     return 
    console.log '4' 
    return 

型号

mongoose = require("mongoose") 

module.exports = mongoose.model("Calendar", 
    name: String 
) 

为什么这是任何想法?

+0

当Mongoose回调未被调用时,通常是因为该模型的连接未打开。你的'mongoose.connect'调用是否成功? – JohnnyHK 2014-12-06 14:42:58

+0

我该如何检查? – Tarlen 2014-12-06 15:24:57

+0

为你的'mongoose.connect'调用提供一个回调函数参数。 – JohnnyHK 2014-12-06 16:00:08

回答

8

在您致电mongoose.connect之前,您的猫鼬式查询只会排队等候。在你的启动代码

添加这样的代码来连接:

mongoose.connect('mongodb://localhost/test', function(err) { 
    if (err) { 
     console.err(err); 
    } else { 
     console.log('Connected'); 
    }  
}); 

在连接字符串中,用你的数据库名称替换test