error while connecting to database TypeError: Invalid URL in Mongo DB

1.7k views Asked by At

When I am trying to connect mongo db cluster with my application it is showing this error enter image description here

I am using URL for Node.js driver of version 2.2.12 or higher

Here is the code of my db.js file

import mongoose from 'mongoose'; //mongoose library is used to form connection between your application and your database mongodb could also be used for the same

const Connection = async () => {
  const URL = `mongodb://chirag_79:[email protected]:27017,ac-jqvhtzt-shard-00-01.nt4mr7j.mongodb.net:27017,ac-jqvhtzt-shard-00-02.nt4mr7j.mongodb.net:27017/?ssl=true&replicaSet=atlas-mbzzwi-shard-0&authSource=admin&retryWrites=true&w=majority`;
  try {
    await mongoose.connect(URL, { useNewUrlParser: true }); //connect function takes two arguments one is the URL and another one is to save us from the error of old urlParser being deprecated
    //old URL parser is deprecated so we will use a new URL parser
    console.log('database connected successfully');
  } catch (error) {
    console.log(`error while connecting to database ${error}`);
  }
};

export default Connection;

Here is the URL to my mongo db cluster: mongodb://chirag_79:[email protected]:27017,ac-jqvhtzt-shard-00-01.nt4mr7j.mongodb.net:27017,ac-jqvhtzt-shard-00-02.nt4mr7j.mongodb.net:27017/?ssl=true&replicaSet=atlas-mbzzwi-shard-0&authSource=admin&retryWrites=true&w=majority

1

There are 1 answers

0
Chirag Dogra On

The issue was in the password

if you'll use special characters as the password and pass them as it is in the URL then it will create issues.

I changed my password and the problem got fixed.