[英]connecting mongoose to mongoDB atlas and nodejs
這是我的貓鼬連接代碼:
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);
});
我收到以下錯誤,知道如何解決這個問題嗎?
錯誤連接 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)]:{}}
只需轉到 atlas 網站安全選項卡並編輯用戶的密碼,並確保您不使用“@”或“:”。 就是這樣。
您需要在連接字符串中對密碼進行編碼:
const connectionString = `mongodb://yourUsername:${encodeURIComponent('yourPassword')}@127.0.0.1:27017/mydb`;
錯誤描述非常清楚 - 您的密碼中有冒號嗎? 典型的連接字符串格式是“mongodb+srv:[username:password@]host1...”,因此未轉義的冒號會引發解析錯誤。
我也有同樣的問題,即使它們很明顯地被轉義,也有“未轉義的冒號”。 嘗試這個:
var uri = encodeURI('mongodb+srv://Sarthak:*******:Wb@cluster0-jli2a.mongodb.net/test?retryWrites=true');
也嘗試使用這種連接方式
/* 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);
});
否則需要考慮的事情:
輸入連接字符串時,我使用 MongoDB Compass 獲得了“授權部分中未轉義的冒號” 。
其中——
我進入“創建免費集群”按鈕並創建了一個集群。
然后我得到了連接字符串。 但是,在輸入可用連接字符串時出現錯誤。
我只是通過登錄 mongodb atlas 更改了密碼。
這是我使用的程序-->
[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.
我按照上述步驟操作,並且能夠登錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.