MongooseError:操作 'featureds.find()` 缓冲在 10000 毫秒后超时

问题描述 投票:0回答:5

我在 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()
而我从未在代码中的任何地方使用过 features 一词。

javascript node.js mongodb mongoose
5个回答
3
投票

对于任何可能偶然发现这一点的人:我的问题与连接错误有关,我设法使用

mongoose.connect
而不是
mongoose.createConnection
来修复它。

请注意 Mongoose 文档说

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

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


3
投票

如果您在本地主机上,请使用

mongoose.connect('mongodb://localhost:27017/myapp');

尝试使用 127.0.0.1 而不是 localhost

mongoose.connect('mongodb://127.0.0.1:27017/myapp');

0
投票

快速修复:

  • 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()
10000ms 后缓冲超时,

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

0
投票

如果有人使用Mongo db Atlas,那么他们需要将其IP地址列入白名单以授权访问。

授权访问的步骤。

  • 获取您的系统IP地址

对于 Mac 用户: 在终端上点击此命令。

curl ifconfig.me

对于 Windows 用户: 在命令提示符上点击此命令。

ipconfig /all

您也可以通过网络找到您的 IP 地址。例如。 https://whatismyipaddress.com/

获得网络 IP 地址后:

转到 Mongo DB Atlas -> 网络访问 -> IP 访问列表 - 添加您的 IP 地址。 您可以共享对特定 IP 地址的访问,也可以对所有人保持开放访问。


0
投票

我一切正常,包括将我的 IP 地址白名单设置为

0.0.0.0/0
并且连接设置得很好。由于某种原因,当我尝试运行我的
server.mjs
模块(我在其中创建服务器并通过 mongoose 建立与 mongoDB 的连接)时,它将在运行
find()
之前使用
server.mjs
开始运行我的其他文件之一。

一个非常老套的解决方案,但我只是使用

find()
将函数移动到文件末尾,以便
server.mjs
运行,从而首先建立连接。

这不应该发生,我仍然对为什么

server.mjs
没有先运行感到困惑,但这个解决方案对我有用。 希望有帮助!!

© www.soinside.com 2019 - 2024. All rights reserved.