Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js {MongoTimeoutError:服务器选择在30000毫秒后超时_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js {MongoTimeoutError:服务器选择在30000毫秒后超时

Node.js {MongoTimeoutError:服务器选择在30000毫秒后超时,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在基本操作系统上制作一个library nodejs应用程序。My server.js: if (process.env.NODE_ENV !== 'production') { require('dotenv').config(); } const mongoose = require('mongoose') mongoose.connect(process.env.DATABASE_URL, { useCreateIndex: true, useNe

我正在基本操作系统上制作一个library nodejs应用程序。My 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运行devStart
(我在package.json中有:“devStart”:“nodemon server.js”) 我明白了”

"

我安装了猫鼬。
可能是什么问题?

如果MongoDB服务器未运行或MongoDB URL不正确,则会发生此错误。您的MongoDB是本地安装的还是云上安装的?还请使用ROBO 3T或compass检查您与mongo的连接。
-谢谢

您应该首先检查MongoDB是否正在运行?其次,如果它不在本地主机上,请在mongod.conf文件的bindIp选项中添加公共IP,然后重新启动MongoDB服务器

bindIp:[公共知识产权]


并确保输入正确的mongoDB URL。

检查mongo是否正在运行。有人会如何检查它是否正在运行?有几种方法,但我建议使用ROBO 3T。创建连接,然后按测试。如果mongoDB正在运行,则应该连接,但如果存在防火墙问题或mongoDB未运行连接,则不会连接建立。
{ 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.