NextJS won't connect to MongoDB when built and put on a server

I have NextJS app that has backend routes inside where I connect to the database. Everything works fine locally and I don’t get any errors but as soon as I deploy the app to the server I start getting this error:

Error connecting to the database: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://mongodb.prakticum-team.ru/docs/atlas/security-whitelist/

I have whitelisted my IP address, IP address of my website and also whitelisted all IP addresses with 0.0.0.0/0 but the error persists.

1 Like

I have solved this issue by calling my host provider and asking them to allow me a specific port so that my website can connect to it. The port was 27017

Solved.
In my case, Im deplying NextJS v14.0.2.
Downgrading packages
mongoose 8.3.4 → 8.1.0
mongodb 6.10.0 → 6.3.0
Solved the five days pain in the head.
NodeJS 12.13.0
Regards!