I'm having problems connecting to my mongoDB Database

Tell us what’s happening:
Hey. So i’m following this tutorial by Bo ( sorry if i spelt the name wrong) BUILDING WEB APPS WITH MERN STACK an i have a problem with connecting mongoDB from my server .js file . Sorry for the long error message. And i even tried the connect the simpler version free code camp gave me but i doesn’t work. What is the way around this?

Your code so far

        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 establish successfully");
})

Then in a file called .env

ATLAS_URI=mongodb+srv://<removed my username>:<removed my password>@<name of my cluster>-pk9on.mongodb.net/<dbname>?retryWrites=true&w=majority

Your browser information:

(node:6148) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
    at parseConnectionString (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\uri_parser.js:547:21)
    at connect (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\operations\connect.js:277:3)
    at C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\mongo_client.js:222:5
    at maybePromise (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\utils.js:719:3)
    at MongoClient.connect (C:\Users\USER\Desktop\My first full
app\backend\node_modules\mongodb\lib\mongo_client.js:218:10)
    at C:\Users\USER\Desktop\My first full app\backend\node_modules\mongoose\lib\connection.js:712:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongoose\lib\connection.js:709:19)
    at Mongoose.connect (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongoose\lib\index.js:335:15)
    at Object.<anonymous> (C:\Users\USER\Desktop\My first full app\backend\server.js:23:10)
    at Module._compile (internal/modules/cjs/loader.js:1151:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
(node:6148) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:6148) [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.
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Port is activated on 5000
(node:8116) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:8116) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [midata-shard-00-00-pk9on.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to midata-shard-00-00-pk9on.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\connection.js:372:9)
    at Object.onceWrapper (events.js:428:26)
    at TLSSocket.emit (events.js:321:20)
    at net.js:672:12
    at TCP.done (_tls_wrap.js:558:7) {
  name: 'MongoNetworkError'
}]
    at Pool.<anonymous> (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\topologies\server.js:438:11)
    at Pool.emit (events.js:321:20)
    at C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\pool.js:562:14
    at C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\pool.js:1009:9
    at callback (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\connect.js:97:5)
    at C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\connect.js:124:7
    at _callback (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\connect.js:349:5)
    at Connection.errorHandler (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\connect.js:365:5)
    at Object.onceWrapper (events.js:428:26)
    at Connection.emit (events.js:321:20)
    at TLSSocket.<anonymous> (C:\Users\USER\Desktop\My first full app\backend\node_modules\mongodb\lib\core\connection\connection.js:370:12)
    at Object.onceWrapper (events.js:428:26)
    at TLSSocket.emit (events.js:321:20)
    at net.js:672:12
    at TCP.done (_tls_wrap.js:558:7)
(node:8116) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8116) [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.

Challenge: Install and Set Up Mongoose

Link to the challenge:

I was just a whitelist problem on mongoDB.

And on mongoose connect i added unified topology: {useUnifiedTopology: true }
mongoose.connect(myURI, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true});

That just solved it.

2 Likes