Mongoose .find()方法会导致请求挂起 [英] Mongoose .find() method causes requests to hang

查看:183
本文介绍了Mongoose .find()方法会导致请求挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了这条路线,但是对它的任何请求都被卡在'待定'并永远运行。

I have this route defined, but any requests made to it get's stuck on 'pending' and runs forever.

当我记录代码时,我看到 1 后跟 4 ,这意味着find方法中的代码从未被执行

When I log the code, I see 1 followed by 4, which means the code inside the find method never gets executed

  # 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
)

任何关于为什么这样的想法?

Any ideas on why this is?

推荐答案

直到您调用 mongoose.connect ,您的mongoose查询将简单排队。

Until you call mongoose.connect, your mongoose queries will simply be queued up.

在您的启动代码中添加如下代码以连接:

Add code like this in your startup code to connect:

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

在连接字符串中,将 test 替换为您的数据库的名称。

In the connection string, replace test with the name of your database.

这篇关于Mongoose .find()方法会导致请求挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆