[英]connecting mongoose to mongoDB atlas and nodejs
This is my mongoose connection code:这是我的猫鼬连接代码:
mongoose.connect("mongodb+srv://Sarthak:*******:Wb@cluster0-jli2a.mongodb.net/test?retryWrites=true",{ useNewUrlParser: true })
.then(()=>{
console.log("Connected to mongo database");
})
.catch((err)=>{
console.log("Error connecting mongo database",err);
});
I got the errors below, any idea how to fix this?我收到以下错误,知道如何解决这个问题吗?
Error connecting mongo database { MongoParseError: Unescaped colon in authority section at parseConnectionString (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:250:23) at QueryReqWrap.dns.resolveTxt [as callback] (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:126:7) at QueryReqWrap.onresolve [as oncomplete] (dns.js:240:10) name: 'MongoParseError', [Symbol(mongoErrorContextSymbol)]: {} }
错误连接 mongo 数据库 { MongoParseError: Unescaped Colon in authority 部分 parseConnectionString (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:250:23) at QueryReqWrap.dns.resolveTxt [作为回调] (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:126:7) 在 QueryReqWrap.onresolve [as oncomplete] (dns.js:240:10) 名称:'MongoParseError', [符号(mongoErrorContextSymbol)]:{}}
Just go to atlas website security tab and edit the password of the user and make sure you do not use "@" or ":".只需转到 atlas 网站安全选项卡并编辑用户的密码,并确保您不使用“@”或“:”。 That's it.
就是这样。
您需要在连接字符串中对密码进行编码:
const connectionString = `mongodb://yourUsername:${encodeURIComponent('yourPassword')}@127.0.0.1:27017/mydb`;
The error description is pretty clear - do you have a colon in your password?错误描述非常清楚 - 您的密码中有冒号吗? The typical connectionstring format is "mongodb+srv:[username:password@]host1..." so an unescaped colon would throw a parse error.
典型的连接字符串格式是“mongodb+srv:[username:password@]host1...”,因此未转义的冒号会引发解析错误。
I had this same problem, also with "unescaped colons" even though they were very clearly escaped.我也有同样的问题,即使它们很明显地被转义,也有“未转义的冒号”。 Try this:
尝试这个:
var uri = encodeURI('mongodb+srv://Sarthak:*******:Wb@cluster0-jli2a.mongodb.net/test?retryWrites=true');
Try to use this way of connection too也尝试使用这种连接方式
/* I've removed the ":Wb" between your password and @clus... As mongoDB atlas website didn't use that in my generated connection string */
mongoose.connect("mongodb+srv://Sarthak:*******@cluster0-jli2a.mongodb.net/test?retryWrites=true",{ useNewUrlParser: true });
mongoose.connection.on('error', (err) => {
console.error(`Mongoose connection error: ${err}`);
process.exit(1);
});
Otherwise things to consider:否则需要考虑的事情:
I was getting "Unescaped colon in authority section" using MongoDB Compass when entering the connection string.输入连接字符串时,我使用 MongoDB Compass 获得了“授权部分中未转义的冒号” 。
For which -其中——
I went into "Create Free Cluster" button and made a cluster.我进入“创建免费集群”按钮并创建了一个集群。
Then I got the connection string.然后我得到了连接字符串。 However, while entering the available connection string I got the error.
但是,在输入可用连接字符串时出现错误。
I just changed the password by logging into mongodb atlas.我只是通过登录 mongodb atlas 更改了密码。
Here is the procedure I used -->这是我使用的程序-->
[1] To change the password - click on encode URI
[2] It will take you here -->
[3] Click on the edit button from the screen above and change your password.
I followed the above steps and was able to login.我按照上述步骤操作,并且能够登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.