无法通过节点Rest应用上的猫鼬连接到mongodb-altas,连接失败:“ UnhandledPromiseRejectionWarning:错误:连接EACCES”

时间:2019-04-12 03:50:56

标签: javascript mongoose mongodb-atlas

我在mongodb-altas上创建了一个新集群,选择Aws-孟买地区,创建用户,允许从任何地方访问,复制“节点3或更高版本”的连接字符串。当尝试从我的节点Express Rest应用建立连接时,出现以下异常。

const mongoose=require('mongoose');
mongoose
.connect(
"mongodb+srv://anky7493:<MYPASSWORDISHERE>@dreamjob- 
i84sb.mongodb.net/test?retryWrites=true"
)
.then(() => {
console.log("Connected to database!");
})
.catch((error) => {
console.log("Connection failed!");
console.log(error);
});

运行应用程序> nodemon索引,以下是错误

[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index index.js`
(node:9504) DeprecationWarning: current URL string parser is deprecated,         
and will be removed in a future version. To use the new parser, pass     
option { useNewUrlParser: true } to MongoClient.connect.
now listening for request !
Connection failed!
{ Error: connect EACCES 13.127.231.212:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
errno: 'EACCES',
code: 'EACCES',
syscall: 'connect',
address: '13.127.231.212',
port: 27017 }

谁能告诉我如何与mongo-altas建立联系。 预先感谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试使用{useNewUrlParser:true}选项,就像它说的那样?


const mongoose=require('mongoose');
mongoose
.connect(
"mongodb+srv://anky7493:<MYPASSWORDISHERE>@dreamjob- 
i84sb.mongodb.net/test?retryWrites=true"
, { useNewUrlParser: true })
.then(() => {
console.log("Connected to database!");
})
.catch((error) => {
console.log("Connection failed!");
console.log(error);
});