问题
I am trying to send a post request from Postman to a Heroku app but it keeps returning a 503 status error
with code = H12
. The app works fine in localhost.
With regards to the database, I have a cluster (M0) in Atlas and I am using the same non-srv connection string that I am using to connect Compass to the same cluster in Atlas.
The only error I see when I run heroku logs
is:
//single line broken into four lines for readability
UnhandledPromiseRejectionWarning:
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.
But I have already added 0.0.0.0/0
in Atlas's IP whitelist.
What could be causing this error?
回答1:
So with the help and assistance of the MongoDB Atlas support team, I managed to fix this by simply using the srv version of the connection string that I got from Atlas to connect with Heroku. After switching the connection string to the srv version, postman was able to make requests without getting the 503 error.
The srv version looks something like this:
mongodb+srv://someClusterAdmin:somePassword@someCluster.vwieg.mongodb.net/someName?retryWrites=true&w=majority
However, MongoDB Compass (v1.21.2) cannot seem to connect with the srv version connection string so I had to continue using the non-srv version for Compass which looks something like this:
mongodb://someClusterAdmin:somePassword@someCluster-shard-00-00.vwieg.mongodb.net:27017,someCluster-shard-00-01.vwieg.mongodb.net:27017,someCluster-shard-00-02.vwieg.mongodb.net:27017/someName?authSource=admin
One issue that I noticed with the current srv version connection string above (which might be happening just for me though so skip this step if the current srv version connection string works fine for you) was that even though it seems to be adding the data that I'm sending from postman to Atlas, I always get the following error:
"code": 79,
"codeName": "UnknownReplWriteConcern",
//some other lines
So with the advise of the MongoDB Atlas support team, I just remove w=majority
from the connection string and that seems to fix that issue.
// &w=majority removed
mongodb+srv://someClusterAdmin:somePassword@someCluster.vwieg.mongodb.net/someName?retryWrites=true
来源:https://stackoverflow.com/questions/62662032/sending-requests-from-postman-to-heroku-returns-503-and-or-unknownreplwriteconce