# Cant connect to MongoDB

**URL:** <https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 29, 2019, 7:08am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429 "2019-07-29T07:08:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksethi2211](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@ksethi2211](https://forum.freecodecamp.org/u/ksethi2211)\
**Post date:** [July 29, 2019, 7:08am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/1 "2019-07-29T07:08:03Z")

</div>

Im following this tutorial: [https://www.youtube.com/watch?v=7CqJlxBYj-M](https://www.youtube.com/watch?v=7CqJlxBYj-M)

When I try to test connection to MongoDB using Mongoose:

I get the following error:

```auto
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Server is running on port: 5000
(node:12972) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-00-knpdv.mongodb.net:27017] on first connect [MongoNetworkError: read ECONNRESET]
    at Pool.<anonymous> (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\topologies\server.js:431:11)
    at Pool.emit (events.js:198:13)
    at connect (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\pool.js:557:14) at callback (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connect.js:109:5)
    at runCommand (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connect.js:129:7)
    at Connection.errorHandler (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connect.js:321:5)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at TLSSocket.<anonymous> (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connection.js:321:10)
    at Object.onceWrapper (events.js:286:20)
    at TLSSocket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:12972) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12972) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

```

My server.js file looks like this

```auto
const express = require('express');

const cors = require('cors');

const mongoose = require('mongoose');

require('dotenv').config();

const app = express();

const port = process.env.PORT || 5000;

app.use(cors());

app.use(express.json());

const uri = process.env.ATLAS_URI;

console.log(uri)

mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true }

);

const connection = mongoose.connection;

connection.once('open', () => {

console.log("MongoDB database connection established successfully");

})

app.listen(port, () => {

console.log(`Server is running on port: ${port}`);

});

```

I have downloaded all the required dependencies and all IPs are whitelisted and have removed \< \> from the password in connection string.

---

<div class="post-metadata">

**Author:** ![Layer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/layer/32/82898_2.png) [@Layer](https://forum.freecodecamp.org/u/Layer)\
**Post date:** [July 29, 2019, 11:02am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/2 "2019-07-29T11:02:45Z")

</div>

Welcome to freecodecamp forum @ksethi2211! 😀

Since everything seemed correct i tried to use your code to connect and it worked without a flaw^^

Most likely there’s something wrong with the atlas uri: you created your own cluster right? Did you also replaced the username:password with your actual username and password ( not only removing the angular brackets)?

---

<div class="post-metadata">

**Author:** ![ksethi2211](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@ksethi2211](https://forum.freecodecamp.org/u/ksethi2211)\
**Post date:** [July 29, 2019, 11:05am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/3 "2019-07-29T11:05:32Z")

</div>

yup i did that @Layer . can u check with this once:

`ATLAS_URI=mongodb+srv://ksethi2211:qwerty123@cluster0-knpdv.mongodb.net/test?retryWrites=true&w=majority`

---

<div class="post-metadata">

**Author:** ![husseyexplores](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/husseyexplores/32/100732_2.png) [@husseyexplores](https://forum.freecodecamp.org/u/husseyexplores)\
**Post date:** [July 29, 2019, 11:45am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/4 "2019-07-29T11:45:32Z")

</div>

Try this one:

```auto
ATLAS_URI=mongodb+srv://ksethi2211:qwerty123@cluster0-knpdv.mongodb.net/test

```

---

<div class="post-metadata">

**Author:** ![ksethi2211](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@ksethi2211](https://forum.freecodecamp.org/u/ksethi2211)\
**Post date:** [July 29, 2019, 11:58am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/5 "2019-07-29T11:58:26Z")

</div>

nope same problem occurs:😭

```auto

(node:16820) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-00-knpdv.mongodb.net:27017] on first connect [MongoNetworkError: read ECONNRESET]
    at Pool.<anonymous> (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\topologies\server.js:431:11)
    at Pool.emit (events.js:198:13)
    at connect (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\pool.js:557:14)
    at callback (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connect.js:109:5)
    at runCommand (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connect.js:129:7) at Connection.errorHandler (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connect.js:321:5)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at TLSSocket.<anonymous> (C:\Users\ksethi014\projects\mernexercisetracker\backend\node_modules\mongodb-core\lib\connection\connection.js:321:10)
    at Object.onceWrapper (events.js:286:20)
    at TLSSocket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:16820) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16820) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

```

---

<div class="post-metadata">

**Author:** ![husseyexplores](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/husseyexplores/32/100732_2.png) [@husseyexplores](https://forum.freecodecamp.org/u/husseyexplores)\
**Post date:** [July 29, 2019, 12:01pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/6 "2019-07-29T12:01:57Z")

</div>

Have you whitelisted your IP in from the Atlas dashboard?  
Either you can whitelist your own ip, or temporarily allow access to every IPs like so:  
[![](https://i.stack.imgur.com/hNZG6.png) ](https://i.stack.imgur.com/hNZG6.png)  
[![](https://i.stack.imgur.com/BOu0S.png) ](https://i.stack.imgur.com/BOu0S.png)

---

<div class="post-metadata">

**Author:** ![ksethi2211](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@ksethi2211](https://forum.freecodecamp.org/u/ksethi2211)\
**Post date:** [July 29, 2019, 12:04pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/7 "2019-07-29T12:04:30Z")

</div>

yaa for now everyone has access to the project.

i had entered 0.0.0.0/0 before only. Can u access the project using your console and the above code?

---

<div class="post-metadata">

**Author:** ![husseyexplores](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/husseyexplores/32/100732_2.png) [@husseyexplores](https://forum.freecodecamp.org/u/husseyexplores)\
**Post date:** [July 29, 2019, 12:09pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/8 "2019-07-29T12:09:55Z")

</div>

Just tested this on glich, and it’s working.  
You can fork it from here: [https://glitch.com/edit/#!/remix/few-question](https://glitch.com/edit/#!/remix/few-question)

---

<div class="post-metadata">

**Author:** ![ksethi2211](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@ksethi2211](https://forum.freecodecamp.org/u/ksethi2211)\
**Post date:** [July 29, 2019, 12:14pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/9 "2019-07-29T12:14:35Z")

</div>

i dont know it still shows the same error for me. it might be a issue with my system.

---

<div class="post-metadata">

**Author:** ![Layer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/layer/32/82898_2.png) [@Layer](https://forum.freecodecamp.org/u/Layer)\
**Post date:** [July 29, 2019, 1:03pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/10 "2019-07-29T13:03:57Z")

</div>

> can u check with this once: […]

> Just tested this on glich, and it’s working.

Yeah, it worked on my side too 😕 ( now change your password asap 😛 )

On the .env file there is no need of quotes around the strings (just a random source of error). Do uri prints correctly when you run the file?

---

<div class="post-metadata">

**Author:** ![athagar](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@athagar](https://forum.freecodecamp.org/u/athagar)\
**Post date:** [August 21, 2019, 1:47pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/11 "2019-08-21T13:47:21Z")

</div>

Did you ever find a fix for this? I’ve got the same issue, enabled all IPs, nothing.

---

<div class="post-metadata">

**Author:** ![konouvang](https://avatars.discourse-cdn.com/v4/letter/k/d2c977/32.png) [@konouvang](https://forum.freecodecamp.org/u/konouvang)\
**Post date:** [September 26, 2019, 8:26pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/12 "2019-09-26T20:26:25Z")

</div>

I ran into the same problem too. I went ahead and looked up Mongodb ATLAS and I used this instead:

```auto
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
 // perform actions on the collection object
  client.close();
});

```

I finally got it to connect. I know you will be able to see the password but I will put that into an env file later. I hope this helps.

---

<div class="post-metadata">

**Author:** ![sfiller23](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@sfiller23](https://forum.freecodecamp.org/u/sfiller23)\
**Post date:** [December 1, 2019, 10:21am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/13 "2019-12-01T10:21:42Z")

</div>

looks good but didnt work for me

---

<div class="post-metadata">

**Author:** ![sfiller23](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@sfiller23](https://forum.freecodecamp.org/u/sfiller23)\
**Post date:** [December 1, 2019, 10:28am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/14 "2019-12-01T10:28:22Z")

</div>

![image](https://global.discourse-cdn.com/freecodecamp/original/3X/c/7/c7a4edf014e060a0b1789a1e0f10c7ad3fa2d8ea.png)

why to write 0.0.0.0/0 ?

---

<div class="post-metadata">

**Author:** ![jsp.dio](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsp.dio/32/134405_2.png) [@jsp.dio](https://forum.freecodecamp.org/u/jsp.dio)\
**Post date:** [February 23, 2020, 4:49am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/15 "2020-02-23T04:49:37Z")

</div>

I had same issue, but it works now ! What I had done which I changed connection string with originally from Atlas and change white IP to 0.0.0.0/0

this is my connection string:

```auto
// Database Connection
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<user>:<password>@bumimakh1-awitx.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { 
    useNewUrlParser: true,
    useUnifiedTopology: true 
});
console.log('Connected to Atlas');
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

```

I hope this help

---

<div class="post-metadata">

**Author:** ![ipanatsia](https://avatars.discourse-cdn.com/v4/letter/i/7c8e57/32.png) [@ipanatsia](https://forum.freecodecamp.org/u/ipanatsia)\
**Post date:** [May 11, 2020, 10:40pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/16 "2020-05-11T22:40:23Z")

</div>

It worked for me, thanks a lot

---

<div class="post-metadata">

**Author:** ![sonikaa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sonikaa/32/134568_2.png) [@sonikaa](https://forum.freecodecamp.org/u/sonikaa)\
**Post date:** [May 25, 2020, 5:36pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/17 "2020-05-25T17:36:41Z")

</div>

Thanks a lot,  
It worked for me.

---

<div class="post-metadata">

**Author:** ![mauroere](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mauroere/32/134249_2.png) [@mauroere](https://forum.freecodecamp.org/u/mauroere)\
**Post date:** [June 6, 2020, 2:56am UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/18 "2020-06-06T02:56:11Z")

</div>

Enter “Network Access” in the MongoDB Dashboard, change the Database User Pass and try again.

---

<div class="post-metadata">

**Author:** ![lisagodenick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lisagodenick/32/201492_2.png) [@lisagodenick](https://forum.freecodecamp.org/u/lisagodenick)\
**Post date:** [January 24, 2021, 8:20pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/19 "2021-01-24T20:20:19Z")

</div>

I am having a similar problem. Here is my code for server.js

```auto
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");

require("dotenv").config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });
const connection = mongoose.connection;
connection.once("open", () => {
  console.log("MongoDB database connection established successfully!");
});

app.listen(port, () => {
  console.log(`Server is running on port: ${port}`);
});

```

My terminal says:  
TypeError: connection.once is not a function  
at Object. (C:\Users\Lisa\Desktop\exercise-tracker\mern-exercise-tracker\backend\server.js:16:12)

I think my other problem is this:  
In my .env file, I have pasted the ATLAS\_URI= connection string from my mongodb Atlas file and it still isn’t working. I did not yet change the – I can’t figure out what the name should be! Can anyone help?

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [January 24, 2021, 8:20pm UTC](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429/20 "2021-01-24T20:20:50Z")

</div>

Closed on January 24, 2021, 8:20pm UTC



[Next page](https://forum.freecodecamp.org/t/cant-connect-to-mongodb/299429.md?page=2)
