【问题标题】:MongooseError: Operation 'featureds.find()` buffering timed out after 10000msMongooseError:操作“featureds.find()”缓冲在 10000 毫秒后超时
【发布时间】:2021-04-01 17:18:03
【问题描述】:

我在 MongoDB 上有一个集合,我正在尝试使用 find() 查询其中的所有元素:

const mongoose = require('mongoose');
const Featured = mongoose.model('featured');
module.exports = app => {
    app.get('/api/featured', async (req, res) => {
      console.log("featured route");
      const featured = await Featured.find();
      console.log(featured);
      res.send(featured);
    })
}

这是 Featured.js:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const featuredSchema = new Schema({});

mongoose.model('featured', featuredSchema);

但是,我在提出请求时遇到了错误:

(node:75568) UnhandledPromiseRejectionWarning: MongooseError: Operation `featureds.find()` buffering timed out after 10000ms
    at Timeout.<anonymous> (/Users/prikshetsharma/Desktop/humboiserver/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:184:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(node:75568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

如何修复此错误并让所有集合项返回find()?奇怪的是,错误显示为featureds.find(),而我从未在任何地方的代码中使用过特色词。

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    快速修复:

    • Featured.js中导出模型:
    const mongoose = require('mongoose');
    const { Schema } = mongoose;
    const featuredSchema = new Schema({}, { collection: "featured" });
    module.exports = mongoose.model('featured', featuredSchema);
    

    UnhandledPromiseRejectionWarning:未处理的承诺拒绝,

    • 您需要将服务代码包装在 try catch 块中,
    const mongoose = require('mongoose');
    // correct this path to your original path of Featured.js
    const Featured = require('./Featured.js');
    app.get('/api/featured', async (req, res) => {
      try {
        console.log("featured route");
        const featured = await Featured.find();
        console.log(featured);
        res.send(featured);
      }
      catch(e) {
        console.log('Catch an error: ', e)
      } 
    });
    

    featureds.find() 缓冲在 10000 毫秒后超时,

    • 会有很多可能性,
    1. 从 node_module 和 *.json 文件中删除 mongoose 模块,重新安装 mongoose 模块并重试。
    2. 检查您是否已连接数据库,然后检查您的数据库集群是否具有正确的网络访问权限。

    【讨论】:

    • 我已经从 package.json 重新安装并编辑了 mongoose。数据库也已连接。我认为问题在于它显示的是 features.find() 而不是 features.find(),我不知道为什么会这样,因为集合的名称是有特色的,而我在任何地方都没有使用过特色这个词。
    • 在尝试捕获后,我还得到以下信息:nhandledPromiseRejectionWarning: ReferenceError: features is not defined
    • 我已经更新了答案,从 Featured.js 导出模型并使用 require('Featured.js')
    • 错误消失了,但这会返回一个空数组,而不是集合中包含的元素。
    • 当创建一个新的精选时,收藏被保存为精选,而不是在精选中。
    【解决方案2】:

    对于其他可能偶然发现此问题的人:我的问题与连接错误有关,我设法通过使用 mongoose.connect 而不是 mongoose.createConnection 来解决它。

    请注意Mongoose documentation saying:

    如果你使用没有连接的模型,Mongoose 默认不会抛出任何错误。

    ...这只会导致缓冲超时。

    【讨论】:

    • mongoose.connect 不会允许多重连接?我将不得不使用 mongoose.createConnection 代替。那这种情况怎么解决呢?
    猜你喜欢
    • 2021-08-23
    • 2021-11-15
    • 2021-12-08
    • 2021-06-30
    • 2021-04-01
    • 2021-04-12
    • 2021-04-11
    • 1970-01-01
    • 2022-11-28
    相关资源
    最近更新 更多