簡體   English   中英

MongooseError:操作“featureds.find()”緩沖在 10000 毫秒后超時

[英]MongooseError: Operation 'featureds.find()` buffering timed out after 10000ms

我在 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 字。

對於可能偶然發現此問題的任何其他人:我的問題與錯誤連接有關,我設法通過使用mongoose.connect而不是mongoose.createConnection來修復它。

請注意Mongoose 文檔說

如果您在沒有連接的情況下使用 model,默認情況下 Mongoose 不會拋出任何錯誤。

...這只會導致緩沖超時。

如果你在本地主機上,使用

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

嘗試使用127.0.0.1而不是localhost

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

快速修復:

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

UnhandledPromiseRejectionWarning:未處理的 promise 拒絕,

  • 您需要將服務代碼包裝在 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)
  } 
});

features.find featureds.find()緩沖在 10000 毫秒后超時,

  • 會有很多種可能,
  1. 從 node_module 和 *.json 文件中刪除 mongoose 模塊,重新安裝 mongoose 模塊並重試。
  2. 檢查您是否已連接到數據庫,然后檢查您是否具有對數據庫集群的正確網絡訪問權限。

如果有人在使用Mongo db Atlas ,那么他們需要將他們的 IP 地址列入白名單以授權訪問。

授權訪問的步驟。

  • 獲取您的系統 IP 地址

對於Mac 用戶:在終端上點擊此命令。 curl ifconfig.me

對於Windows 用戶:在命令提示符下點擊此命令。 ipconfig /all

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

一旦你有了你的.networks IP 地址:

Go 到 Mongo DB Atlas -> 網絡訪問 -> IP 訪問列表 - 添加您的 IP 地址。 您可以共享對特定 IP 地址的訪問權限,也可以對所有人保持開放訪問權限。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM