![](/img/trans.png)
[英]MongooseError: Operation `.insertOne()` buffering timed out after 10000ms
[英]MongooseError: Operation `urls.find()` buffering timed out after 10000ms
我正在嘗試發出一個帖子請求,將查找數據保存在 MongoDB 中,或者如果它不存在則創建一個,然后我收到這樣的錯誤
MongooseError: Operation `urls.find()` buffering timed out after 10000ms
我的 main.js
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
// Basic Configuration
const port = process.env.PORT || 3000;
app.use(cors());
app.use("/public", express.static(`${process.cwd()}/public`));
app.get("/", function (req, res) {
res.sendFile(process.cwd() + "/views/index.html");
});
// Your first API endpoint
app.get("/api/hello", function (req, res) {
res.json({ greeting: "hello API" });
});
app.listen(port, function () {
console.log(`Listening on port ${port}`);
});
mongoose.connect(
process.env.MONGODB_CONNECT,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
},
() => {
console.log("Connected to mongoDB");
}
);
let urlSchema = new mongoose.Schema({
original: { type: String, required: true },
short: Number,
});
let Url = mongoose.model("Url", urlSchema);
let responseObject = {};
app.post(
"/api/shorturl/new",
bodyParser.urlencoded({ extended: false }),
(request, response) => {
let inputUrl = request.body["url"];
responseObject["original_url"] = inputUrl;
let inputShort = 1;
Url.findOne({})
.sort({ short: "desc" })
.exec((error, result) => {
// THE ERROR APPEARS
console.log(error);
if (!error && result != undefined) {
inputShort = result.short + 1;
}
if (!error) {
Url.findOneAndUpdate(
{ original: inputUrl },
{ original: inputUrl, short: inputShort },
{ new: true, upsert: true },
(error, savedUrl) => {
if (!error) {
responseObject["short_url"] = savedUrl.short;
response.json(responseObject);
}
}
);
}
});
}
);
我的依賴
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.11.10"
我已經在類似的問題中嘗試了一些解決方案,例如:
刪除 node_module 文件並重新安裝 mongoose
將MongoDB集群中nodejs的版本更改為2.2.12或更高版本
將我的 IP 更改為 google public DNS 8888 8844
任何幫助將非常感激
我確實嘗試設置一個新環境並使用相同的 MongoDB 數據庫,結果我的數據庫出現問題可能是連接問題,我嘗試創建一個新項目並從任何地方設置訪問權限並選擇靠近我的位置的服務器,它現在工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.