无法使用 mongoose.js 连接到 mongoDB atlas

无法使用 mongoose.js 连接到 mongoDB atlas

我无法使用 mongoose.js 连接到 mongoDB atlas 

它引发了错误:

C:\Users\uttar\Desktop\blog_application>node index.js
including files:./routes/appRoutes.js
including files:./models/BlogSchema.js
Example app listening on port 3000!
database connection error:
{ Error: querySrv ETIMEOUT _mongodb._tcp.cluster0-t3rcj.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:196:19)
  errno: 'ETIMEOUT',
  code: 'ETIMEOUT',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.cluster0-t3rcj.mongodb.net' }

[这是控制台中的错误快照]

app.listen(port, () => {
    console.log(`Example app listening on port ${port}!`);
    let db = mongoose.connect(mongodb+srv://amiuttar:<[password>][1]@cluster0-t3rcj.mongodb.net/test?retryWrites=true, {useNewUrlParser: true});

})

//handling database connection error

mongoose.connection.on('error',function (err){
    console.log("database connection error:");
    console.log(err);
})

//handling connection success

mongoose.connection.on('open',function (err){
    if(err){
        console.log("database connection error:");
        console.log(err); 
    }else{
        console.log("database connected successfully");
    }
})

相关内容