Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to MongoDB because of wrong URI

I was trying to run mongoDB on node server Full Code here from MongoDB:

My mongo version: 4.4.3

Node version: v15.7.0

I've imported get started code from MongoDB, and here's the code:

const { MongoClient } = require("mongodb");
// Connection URI
const uri =
  "mongodb+srv://sample-hostname:27017/?poolSize=20&writeConcern=majority";
// Create a new MongoClient
const client = new MongoClient(uri);
async function run() {
  try {
    // Connect the client to the server
    await client.connect();
    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully to server");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

On terminal, when i run "node app.js", it throws me following error:

> (node:79653) Warning: Accessing non-existent property 'MongoError' of
> module exports inside circular dependency (Use `node --trace-warnings
> ...` to show where the warning was created) MongoParseError: URI does
> not have hostname, domain name and tld
>     at parseSrvConnectionString (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:50:21)
>     at parseConnectionString (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:594:12)
>     at connect (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/operations/connect.js:284:3)
>     at /home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:225:5
>     at maybePromise (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:681:3)
>     at MongoClient.connect (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:221:10)
>     at run (/home/harmony/Desktop/FruitsProject/app.js:12:18)
>     at Object.<anonymous> (/home/harmony/Desktop/FruitsProject/app.js:21:1)
like image 677
Thirumalai vasan Avatar asked Feb 04 '21 16:02

Thirumalai vasan


People also ask

How do I access MongoDB URI?

Find MongoDB URIClick on “Overview” tab in the menu bar. Scroll down the Overview page and you will see the MongoDB URI information.

Why is my MongoDB not connecting?

Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

What is MongoDB URI?

The URI describes the hosts to be used and options. The format of the URI is: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]] mongodb:// is a required prefix to identify that this is a string in the standard connection format.

Why is MongoDB not connecting to Atlas?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.


Video Answer


4 Answers

The error Accessing non-existent property 'MongoError' of > module exports inside circular dependency is caused by a bug in mongodb 3.6.4

It's already reported here

Back to version 3.6.3 works for me:

npm uninstall mongodb --save

Install version 3.6.3

npm i [email protected]
like image 128
ztom Avatar answered Oct 23 '22 19:10

ztom


For everyone searching about this warning, don't worry, it's just a version bug, and was already reported. Just uninstall the 3.6.4 version and install the version 3.6.3 as answered on @kmgt answer.

More details:
https://developer.mongodb.com/community/forums/t/warning-accessing-non-existent-property-mongoerror-of-module-exports-inside-circular-dependency/15411

https://github.com/Automattic/mongoose/issues/9900

like image 27
Dr4kk0nnys Avatar answered Oct 23 '22 20:10

Dr4kk0nnys


I downgraded MongoDB as recommended but that alone didn’t solve the problem.

I had to downgrade mongoose too to get the error removed.

I downgraded to:

MongoDB version 3.6.3 Mongoose version 5.11.15

like image 9
mcmedal001 Avatar answered Oct 23 '22 19:10

mcmedal001


You don't have to downgrade your MongoDB, just downgrade your mongoose package to 5.11.15 as in this mention: https://github.com/Automattic/mongoose/issues/9900#issuecomment-778535254 I'm using Mongo v4.4.3 and just downgrade mongoose worked for me.

like image 1
Duc Tran Avatar answered Oct 23 '22 20:10

Duc Tran