[英]MongoDB refusing to connect using NodeJS
I have been struggling to connect to MongoDB for the past few hours now.在过去的几个小时里,我一直在努力连接到 MongoDB。 I have taken the code from the atlas documentation as seen below:我从 atlas 文档中获取了代码,如下所示:
const { MongoClient } = require('mongodb')
const url =
'mongodb+srv://user:pass@mmmcluster.axapu.mongodb.net/appname?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true'
const client = new MongoClient(url)
async function run() {
try {
await client.connect()
console.log('Connected correctly to server')
} catch (err) {
console.log(err.stack)
} finally {
await client.close()
}
}
run().catch(console.dir)
I cannot for the life of me figure out why I keep getting the error below:我一生都无法弄清楚为什么我不断收到以下错误:
MongoServerSelectionError: connect ETIMEDOUT 157.241.16.152:27017
at Timeout._onTimeout (E:\Documents\Github\@test\node_modules\mongodb\lib\sdam\topology.js:306:38)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
For the record:作为记录:
Any suggestions?有什么建议么? I am out of ideas here and I don't want to have to install Compass as a workaround since ideally, all methods of connection should work.我在这里没有想法,我不想安装 Compass 作为解决方法,因为理想情况下,所有连接方法都应该有效。
I think you should take a look here:我认为你应该看看这里:
const url =
'mongodb+srv://user:pass@mmmcluster.axapu.mongodb.net/appname?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true'
since there is this user
and pass
you might want to replace them with the actual username and password for the database.由于存在此user
并pass
,您可能希望将它们替换为数据库的实际用户名和密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.