001 falah
001 falah

Reputation: 21

Node js : Mongoose and mongo connecting problem

While I try to connect mongodb with express js with mongoose and I am getting error for a long. How to fix this is ?

const express = require('express');
const mongoose = require('mongoose');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello from node js')
})

const connection_url = 'connection-url';

mongoose.connect(connection_url, {
    useCreateIndex: true,
    useNewUrlParser: true,
    useUnifiedTopology: true,
}).then(()=> console.log("Database Connected"));

app.listen(3000);

This is the error

node:14460) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
    at NativeConnection.Connection.openUri (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\connection.js:846:32)
    at C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:351:10
    at C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:1149:10)
    at Mongoose.connect (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:350:20)
    at Object.<anonymous> (C:\MyFolder\Coding\DB connection\index.js:11:10)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14460) 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:14460) [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.

How to fix this is and connect to the mongodb ??

Upvotes: 0

Views: 478

Answers (1)

linux2
linux2

Reputation: 265

  1. Go to MongoDB Atlas
  2. Open your cluster
  3. Go to settings > Network Access

enter image description here

  1. Add IP Addresss

enter image description here

  1. Click on Allow access on anywhere || Just add your current IP

  2. Restart your app and it should be working

Upvotes: 1

Related Questions