![](/img/trans.png)
[英]MongooseError: Operation `.insertOne()` buffering timed out after 10000ms
[英]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);
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 毫秒后超時,如果有人在使用Mongo db Atlas ,那么他們需要將他們的 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.