Can't connect to MongoDB

const Express = require("express");
const MongoClient = require("mongodb").MongoClient;
const port= process.env.PORT || 5500;

const CONNECTION_URL = "mongodb+srv://<userName>:<password>@testcluster-e6kkl.mongodb.net/test?retryWrites=true";

const DATABASE_NAME = "example";
var app = Express();
app.use(Express.json());
var database, collection;

app.post("/person", (request, response) => {
  collection.insert(request.body, (error, result) => {
      if(error) {
          return response.status(500).send(error);
      }
      response.send(result.result);
  });
});

app.get("/people", (request, response) => {
  collection.find({}).toArray((error, result) => {
      if(error) {
          return response.status(500).send(error);
      }
      response.send(result);
  });
});

app.listen(port, () => {
    MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {
        if(error) {
            console.log('this is error :',error);
        }
        database = client.db(DATABASE_NAME);
        collection = database.collection("people");
        console.log("Connected to `" + DATABASE_NAME + "`!");
    });
});

Guys I m getting this error in heroku logs =>
MongoNetworkError: connection 5 to testcluster-shard-00-01-e6kkl.mongodb.net:27017 closed.

Checklist =>

  1. IP whitelisting done (using public ip add from google search )
  2. user name & password are correct.
  3. 2 days waste with no success.

is there something like restating windows service for mongodb service?


MOD EDIT: formatting

Hey @speedynik, welcome to the Glitch forum!

I’ve moved your post to it’s own topic, since the issue may be a different one.

I’m not sure what you mean when you say you’re seeing an error in he heroku logs, but for the time being I’m going to assume that you mean in the Logs pane of your Glitch project. Glitch project IP addresses change quite frequently (sometimes as often as every 12 hours), so I’d suggest that the first thing you do is to allow access from anywhere to verify that your whitelist isn’t the problem.

38

If that works then you know you have a whitelist issue and whatever IP addresses you’ve allowed aren’t accurate for your project’s current location.

1 Like

Created an account just to say, thanks for this. I’m doing something unrelated using mLabs and I couldn’t for the life of me figure out what’s wrong. I decided to give this whitelist thing a shot and guess what, I guess my IP wasn’t in the list of allowed ranges.

Thanks again.