node.js - { MongoTimeoutError: 服务器选择在 30000 毫秒后超时
问题描述
我正在 Elementary OS 上制作一个库 nodejs 应用程序。我的 server.js:
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
...........
const mongoose = require('mongoose')
mongoose.connect(process.env.DATABASE_URL, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true, // commented out currently
})
const db = mongoose.connection
db.on('error', error => console.error(error))
db.once('open', () => console.log('Connected to Mongoose'))
app.use('/', indexRouter)
app.use('/authors', authorRouter)
app.use('/books', bookRouter)
app.listen(process.env.PORT || 3000)
每次我运行
npm run devStart
(我在 package.json:“devStart”:“nodemon server.js”)我得到这个“
{ MongoTimeoutError: Server selection timed out after 30000 ms
at Timeout.setTimeout [as _onTimeout] (/media/dan/Alte date/aplicatii nodejs/nodejs_mongo_cc/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)
name: 'MongoTimeoutError',
reason:
{ Error: connect ECONNREFUSED 127.0.0.1:27017
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {} },
[Symbol(mongoErrorContextSymbol)]: {} }
(node:6287) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
at Timeout.setTimeout [as _onTimeout] (/media/dan/Alte date/aplicatii nodejs/nodejs_mongo_cc/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)
(node:6287) 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(). (rejection id: 1)
(node:6287) [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.
"
我安装了猫鼬。可能是什么问题呢?
解决方案
如果 MongoDB 服务器未运行或 mongoDB URL 不正确,则会发生此错误。您的 mongoDB 是安装在本地还是在云上?另请使用 ROBO 3T 或指南针检查您的 mongo 连接。-谢谢
推荐阅读
- r - Tidyverse 安装错误——我该如何解决这个 dplyr 冲突?
- amazon-web-services - 亚马逊AWS | EMR - 未应用配置
- powershell - 如何使用 powershell 在大型 .csv 中输出第一个负值?
- c# - 如何在多个线程中填充数组?
- python - 用布尔索引替换 df 中的 val - pandas
- python - 如何使用 python-gitlab 返回并查看所有 Gitlab 的项目问题信息?
- ember.js - 在 Ember JS 属性中传递多个参数
- django - Django rest框架,批量删除
- c# - 获取用户 SID 抛出 IdentityNotMappedException
- vue.js - Jest / Vue Test Utils - Axios mock 仅适用于测试文件本身