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

查看:44
本文介绍了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天全站免登陆