Abir
Abir

Reputation: 1

Connection Error with MongoDB and Express

Trying to connect my express app with mongodb atlas, but I keep getting this MongodbNetworkError

Server.js file

    const express = require("express")
    const mongoose = require("mongoose")
    const dotenv = require("dotenv")

    dotenv.config()

    const app = express()

    mongoose
     .connect(process.env.MONGODB_KEY, {
        useNewUrlParser: true,
        useUnifiedTopology: true
     })
     .then(() => console.log("MongoDB connected"))
     .catch(err => console.error(err))

    const PORT = process.env.PORT || 5000

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

Command Line

  [nodemon] starting `node server.js`
  Server running on port 5000
  { Error: getaddrinfo ENOTFOUND coolcluster-shard-00-00-wqaqv.mongodb.net coolcluster-shard-00-00- 
  wqaqv.mongodb.net:27017
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

Upvotes: 0

Views: 270

Answers (1)

Shijin TR
Shijin TR

Reputation: 7756

You need to whitelist the IP address of your nodejs server in MongoDB. After login to the account, Follow the steps

Go to IP Whitelist view.
  a)In the Security section of the left navigation, click Network Access. The IP Whitelist tab displays.
  b)Click plus icon Add IP Address.

https://docs.atlas.mongodb.com/security-whitelist/#add-whitelist-entries

Upvotes: 1

Related Questions