Reputation: 484
I deployed an express REST API using visual studio azure extension. It only consists of endpoints that serves JSON data. I have a few endpoints that it only serves and error message, those are working just fine, so the express application is working correctly but when I reach to an endpoint that requires to read from MongoDB, in visual studio, inside the streaming logs it prompts this:
Error:
(node:68) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-02-cia4k.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to cluster0-shard-00-02-cia4k.mongodb.net:27017 closed
So as you can see it says it having a problem connecting, and I don't know what failed to connect to the server, "connection 4"
means in this case.
I'm trying to connect an express application to MongoDB in the cloud. I'm using MongoDB Compass to visualize the data, and it doesn't have a problem connecting. These are the things that I made sure were correct.
mongodb+srv://<MyUsername>:<MyPassword>@cluster0-cia4k.mongodb.net/<DatabaseName>
.?authSource=admin
.I already look at this StackOverflow thread but I couldn't find something that helped me. Most of the problems in that post were locally in their computers.
Please help me.
Upvotes: 1
Views: 436
Reputation: 484
I've found the solution, So I wasn't wrong about one of the steps that I made but it required an extra setup. Basically the problem was this:
Network Access in MongoDB: Azure IP is registered in MongoDB (I found it in the "Custom Domain" tab in azure).
It turns out that Microsoft Azure has multiple outbound IP Addresses. You can find them under the (Setting > Properties) > Outbound IP addresses. Put them all in MongoDB Atlas and that fixed my issue.
Upvotes: 1