【问题标题】:Why mongoose is not connecting to MongoDB Atlas?为什么猫鼬没有连接到 MongoDB Atlas?
【发布时间】:2020-04-08 07:00:07
【问题描述】:

我正在尝试连接到 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)]: {} }

【问题讨论】:

标签: mongodb mongoose


【解决方案1】:

不降级

你能在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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2016-04-29
    • 2019-10-23
    • 1970-01-01
    • 2020-04-03
    • 2018-08-03
    • 2016-07-22
    相关资源
    最近更新 更多