Ever since 5.0 I can't connect to my db

 const mongodb = require('mongodb').MongoClient;
 const connectionString = 'deletedforprivacy';
 
 
 mongodb.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}, function (err, client) {
     module.exports = client.db()
     const app = require('./app')
     app.listen(5000);
})

Is there anything that I am doing wrong?

Yes you are doing something wrong.

You do not provide enough information for us to help you.

Where is mongod running? On localhost, Atlas, … Do you manage the server yourself?

You are hiding your connection string so it is hard to say anything about it. Is there any error message?

Thanks for the reply. I manage the server myself using node express on localhost.

const connectionString = 'mongodb+srv://username:password@cluster0.hr6x6af.mongodb.net/ComplexApp?retryWrites=true&w=majority';

When I put app.listen(5000) inside the anonymous function within mongodb.connect(), I dont get any connectivity to my site at all if I run node db.js in the terminal. But if I put app.listen(5000) back inside the app.js file, my site works.

But the whole point is to connect to the database first which is why I want it inside of the connect() function on my db.js file.

Sorry if I am not explaining this well, I hope this makes sense.

Oh and the only error message is a warning with the connect function on mongodb.connect

It says invalid number of arguments, expected 1 …2
Promise returned from connect is ignored

But I dont think that is whats causing a ERR_CONNECTION_REFUSED on my localhost:5000 page

I downgraded to mongodb 4.13.0 and my code above works.

Hi @Tony_Guarino

I think you hit on a breaking change in Node driver 5.0, especially this part:

This release removes support for callbacks in favor of a promise-based API. The following list provides some strategies for callback users to adopt this version:

  • Migrate to the promise-based API (recommended)
  • Use the promise-based API and util.callbackify
  • Add mongodb-legacy to continue using callbacksFor more information about these strategies, see Changes in the MongoDB Node.js Driver v5.

Since your code example is using a callback style, I believe that’s why it stops working with Node driver 5.0.

Best regards
Kevin

3 Likes

That makes sense. Thanks for letting me know