Cannot connect to MongoDB atlas

Hey guys. I recently deployed my app on Netlify and so far, everything is pretty good; however I cannot connect to my mongoDB atlas. Can anyone guide me on how to connect my create-react-app, express server all to mongo atlas without running into CORS issues? It says the error “has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.” whenever I try to get a get request to my mongo atlas. I’m sure many have dealt with this issue and I’m sure there is a step by step guide; I have tried looking and cannot find one. Please help me out I’m so close to deploying!

Hey @mostwontiwill

So, if your Netlify site was https://awesome-cool.netlify.app you would need to add

Access-Control-Allow-Origin: https://awesome-cool.netlify.app

as a header to your backend.

I know this may sound dumb, but can you further explain where I would put this? Like can you tell me what exactly I would need to put and where?

I put :

res.setHeader('Access-Control-Allow-Origin', '*');

I also have the npm package cors installed and put (even though I think they do the same thing):

app.use(function (req, res, next) {

  res.setHeader('Access-Control-Allow-Origin', '*');


  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');


  res.setHeader('Access-Control-Allow-Credentials', true);

  next();
});

What else am I missing here? Pretty frustrating stuff.

A simpler solution is to use proxy rewrites:

Did you end up coming up with a solution?

Hi, @carolinemae. It has been six months since the last reply here so it is possible they won’t reply.

Also, the question in this topic isn’t actually about Netlify and finding the correct header values to fix CORS errors with a third-party database is not something covered by our scope of technical support.

In other words, the question above isn’t specific to Netlify or about Netlify. I have some examples to clarify.

If you ask this type question below, our support team will be able to answer:

  • How do I create a CORS header with the value X at Netlify? (where X is whatever you want to create)

This is a question about how to use Netlify and we can assist with this. However, the question in this topic was more like this type of question below:

  • What is the correct CORS header to set to connect to this third-party database?

That isn’t a question about Netlify. That is a question about a third-party database. Our support team doesn’t have the resources to troubleshoot third-party software or systems. We also do not troubleshoot the code you write. Netlify troubleshoots the code that we write and the services we provide.

Long story short, while all questions are welcome here this was never a question our support team would have the time to answer because it is outside of our scope of support. Now, someone else (for example, a Forum Pilot like @coelmay) might be willing to answer this type of question and, again, you are welcome to ask it. It is just that this is probably not the most targeted audience for this question.

Searching a more general troubleshooting site (like https://stackoverflow.com/) for answers to CORS header questions might yield better results.

Hi Luke,
Thanks for the response. I am not asking about headers, as this did not appear to fix it for the original poster. I am wondering if original poster may have come to a resolution, as I am having the same issue with Netlify connecting to MongoDB atlas. I have searched and tried all troubleshooting suggested, and yet it doesn’t connect on my Netlify app, but it works seamlessly on Heroku - so I thought it would be a question for Netlify?

Hi, @carolinemae. To answer this question we would need more information. One of the key pieces of information which is missing is how are you trying to connect to MongoDB atlas. Without that information I cannot even being to debug this.

Are you trying to access it during the site build? Are you trying to access it in a Function deployed at Netlify? Are you trying to access it in client-side javascript?

Our support team needs to be able to see the error ourselves to troubleshoot. If you can point us to the deploy logs, the function logs, or to a page on a deployed site where we can see the error in the javascript console - then we can assist with troubleshooting.

If you would please tell us where we can see the error happening, we’ll be ready to assist.