[英]Why are all my Mongoose requests timing out?
我的 ZCCADCDEDB567ABAE643E15DCF0974E503Z 請求從昨天開始就全部超時了。
我的互聯網連接正常,和往常一樣,我的源代碼沒有改變。
所以,我認為這一定是我的依賴項或 MongoDB 本身的問題。
最小的可重現示例:
const mongoose = require('mongoose')
const mongoURL = //replace this comment with your own Mongo URL
mongoose.connect(mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
})
const exampleSchema = new mongoose.Schema({
title: String,
author: String
})
const Example = mongoose.model('Example', exampleSchema)
const exampleOne = new Example({
title: 'Don Quixote',
author: 'M. Cervantes'
})
exampleOne.save().then(res => console.log(res))
mongoose.connection.close()
運行上述示例的完整錯誤跟蹤:
(node:18284) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18284) UnhandledPromiseRejectionWarning: MongooseError: Operation `examples.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (G:\Programming\Courses\Fullstack-Helsinki-2020\mongo_testing\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(node:18284) 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: 1)
(node:18284) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18284) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
我當前的 Mongoose 和 MongoDB 版本(來自 package.json):
"mongoose": {
"version": "5.11.16",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.11.16.tgz",
"integrity": "sha512-qmolyGAskPuq0Xr3j2Tjm9jwRccGGnLRWtTuyRvYBZoyItajwIoQdetJH8oVzs3N7aZK/GKZ82xV/t97suF8Pg==",
"requires": {
"@types/mongodb": "^3.5.27",
"bson": "^1.1.4",
"kareem": "2.3.2",
"mongodb": "3.6.4",
"mongoose-legacy-pluralize": "1.0.2",
"mpath": "0.8.3",
"mquery": "3.2.4",
"ms": "2.1.2",
"regexp-clone": "1.0.0",
"safe-buffer": "5.2.1",
"sift": "7.0.1",
"sliced": "1.0.1"
},
"dependencies": {
"mongodb": {
"version": "3.6.4",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.4.tgz",
"integrity": "sha512-Y+Ki9iXE9jI+n9bVtbTOOdK0B95d6wVGSucwtBkvQ+HIvVdTCfpVRp01FDC24uhC/Q2WXQ8Lpq3/zwtB5Op9Qw==",
"requires": {
"bl": "^2.2.1",
"bson": "^1.1.4",
"denque": "^1.4.1",
"require_optional": "^1.0.1",
"safe-buffer": "^5.1.2",
"saslprep": "^1.0.0"
}
},
"safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
}
}
問題:為什么上面的示例會引發上述錯誤,一般來說,為什么我的 ZCCADCDEDB567ABAE643E15DCF0974E503Z 請求都超時了?
首先,您需要等待建立連接以確保它正常,請參閱錯誤處理:
try {
await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
} catch (error) {
handleError(error);
}
其次,您需要在save
調用將被解決或拒絕后調用mongoose.connection.close()
:
exampleOne.save().then(res => {
console.log(res)
mongoose.connection.close()
})
因為您沒有使用await
save
調用沒有等待解決並且mongoose.connection.close()
被立即調用。
const res = await exampleOne.save()
console.log(res)
mongoose.connection.close()
})
正如我在評論中所說的那樣,@Anatoly 也說你應該在建立連接后發送請求(即保存)。
const mongoose = require('mongoose')
const exampleSchema = new mongoose.Schema({
title: String,
author: String
})
const Example = mongoose.model('Example', exampleSchema)
const mongoURL = //replace this comment with your own Mongo URL
mongoose.connect(mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
})
.then(() => {
const exampleOne = new Example({
title: 'Don Quixote',
author: 'M. Cervantes'
})
exampleOne.save().then(res => {
console.log(res)
mongoose.connection.close()
})
})
.catch(err => {
// handle error
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.