1

I'm trying to deploy a MERN stack application on Heroku based on the mern.io boilerplate code, and to do so I was following this tutorial that is based on the same boilerplate code: https://github.com/jeffreynerona/mern-io-heroku.

I'm currently trying to link my MongoDB cluster to my application, so I used the exact URI generated by MongoDB and I ended with the following code:

const config = {
  mongoURL: process.env.MONGO_URL || 'mongodb+srv://sam:sam1@cluster0-qnroe.mongodb.net/mern-starter',
  port: process.env.PORT || 8000,
};

export default config;

However, this gave me the following error: Error: Invalid mongodb uri "mongodb+srv://sam:sam1@cluster0-qnroe.mongodb.net/mern-starter". Must begin with "mongodb://".

Just as reference, when I was hosting my application locally everything ran as it should. The rest of the code in the block above was the same, except the URI I had for that was mongodb://localhost:27017/mern-starter. Any thoughts would be appreciated on why this new URI is giving me an error!

yassadi
  • 524
  • 1
  • 9
  • 20
Sammd
  • 11
  • 3
  • Possible duplicate of [Fail to connect Mongoose to Atlas](https://stackoverflow.com/questions/48917591/fail-to-connect-mongoose-to-atlas) – Andrew Li Apr 21 '18 at 04:12
  • 1
    You need the 3.x version of the node driver in order to use `mongodb+srv`. If you cloned this project from someone's github repo then it's likely a 2.x driver. Just use the `mongodb://` option instead, and optionally with `replicaSet` appended if you have one. There should be an option to copy this from the Atlas console. – Neil Lunn Apr 21 '18 at 04:16

1 Answers1

-3

The error message you received indicates that the MongoDB URI you provided is invalid because it must begin with "mongodb://" instead of "mongodb+srv://". The "mongodb+srv://" prefix is used for connecting to a MongoDB Atlas cluster, while "mongodb://" is used for connecting to a regular MongoDB deployment.

The difference between the two connection strings is that "mongodb+srv://" is used for connecting to a MongoDB cluster hosted on MongoDB Atlas, which provides a fully managed cloud database service. On the other hand, "mongodb://" is used for connecting to a standalone MongoDB deployment or a MongoDB replica set.

To fix the issue, you need to modify your MongoDB URI to start with "mongodb://" instead of "mongodb+srv://". Here's how your updated code should look:

const config = { mongoURL: process.env.MONGO_URL || 'mongodb://sam:sam1@cluster0-qnroe.mongodb.net/mern-starter', port: process.env.PORT || 8000, };

export default config;

Make sure to replace the sam:sam1 with the correct username and password for your MongoDB cluster. Additionally, ensure that your MongoDB cluster allows incoming connections from your Heroku application by configuring the network access settings in your MongoDB Atlas dashboard.

With these changes, you should be able to connect your MERN stack application to your MongoDB cluster when deploying it on Heroku.

  • wow great answer! I can't believe someone downvoted it. it's really good. just out of curiosity, did you get any of this from chatgpt? – starball Jun 28 '23 at 07:50