Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo DB Connection - Password contains unescaped characters

Tags:

mongodb

I am trying to connect to MongoDB but I am getting the "Password contains unescaped characters" error. I have included the useNewUrlParser:true option but I still get the error See my code below:

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");

dotenv.config();

connectDB().catch(err => console.log(err)); 

async function connectDB() {
await mongoose.connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
})
    .then(() => console.log("DB Connection Success"))
    .catch((err) => console.log(err));
}

app.listen(8800, () => {
console.log("Backend Server is running");
});
like image 225
Daniel Kwame Amissah Avatar asked Oct 12 '25 10:10

Daniel Kwame Amissah


1 Answers

Same problem here... solved with encodeURIComponent on the part of the string that had the password

const uri = "mongodb+srv://username:" + encodeURIComponent("pass#") + "@cluster.yijnb.mongodb.net/db?retryWrites=true&w=majority";
like image 110
Vinícius Melo Avatar answered Oct 14 '25 23:10

Vinícius Melo