首页 > 解决方案 > 为什么猫鼬没有连接到 MongoDB Atlas?

问题描述

我正在尝试连接到 MongoDB Atlas 但我收到此错误

(node:7191) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
{ MongoError: no mongos proxy available
    at Timeout.<anonymous> (/home/gaurav/Downloads/Assignments/DevConnection_2.0/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/mongos.js:739:28)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }

我正在尝试像这样连接

db = "mongodb+srv://<username>:<password>@devconnector-sk8na.mongodb.net/test?retryWrites=true&w=majority" 
try{
    await mongoose.connect(db, { useNewUrlParser: true})            
        console.log("MongoDB Connected..")
} catch(err){
    console.error(err);
    process.exit(1);
}

我还在使用时替换了用户名和密码。

我应该得到这个输出 -

MongoDB connected...

我正在使用,“猫鼬”:“^5.8.1”,

即使我降级到 Mongoose 版本 5.6.13,弃用警告也会消失,但以下错误仍然存​​在 -

{ MongoError: no mongos proxy available
    at Timeout.<anonymous> (/home/gaurav/Downloads/Assignments/DevConnection_2.0/node_modules/mongodb-core/lib/topologies/mongos.js:736:28)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }

标签: mongodbmongoose

解决方案


无需降级

您可以在旁边添加此行useNewUrlParser:true然后再次运行吗?

useUnifiedTopology: true 

您的代码应如下所示

db = "mongodb+srv://<username>:<password>@devconnector-sk8na.mongodb.net/test?retryWrites=true&w=majority" 
try{
    await mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })            
        console.log("MongoDB Connected..")
} catch(err){
    console.error(err);
    process.exit(1);
}

推荐阅读