首页 > 解决方案 > 从 Postman 向 Heroku 发送请求返回 503 和/或 UnknownReplWriteConcern

问题描述

我正在尝试从 Postman 向 Heroku 应用程序发送发布请求,但它一直返回503 status errorwith code = H12。该应用程序在 localhost 中运行良好。

关于数据库,我在 Atlas 中有一个集群 (M0),我使用的非 srv 连接字符串用于将 Compass 连接到 Atlas 中的同一集群。

我运行时看到的唯一错误heroku logs是:

//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.

但是我已经加入0.0.0.0/0了 Atlas 的 IP 白名单。


什么可能导致此错误?

标签: mongodbherokupostmanmongodb-atlasmongodb-compass

解决方案


因此,在 MongoDB Atlas 支持团队的帮助和协助下,我设法通过简单地使用从 Atlas 获得的连接字符串的 srv 版本来连接 Heroku 来解决这个问题。将连接字符串切换到 srv 版本后,邮递员能够发出请求而不会收到 503 错误。

srv 版本如下所示:

mongodb+srv://someClusterAdmin:somePassword@someCluster.vwieg.mongodb.net/someName?retryWrites=true&w=majority

但是,MongoDB Compass (v1.21.2) 似乎无法连接到 srv 版本的连接字符串,所以我不得不继续使用 Compass 的非 srv 版本,看起来像这样:

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

我注意到上面当前 srv 版本连接字符串的一个问题(这可能只发生在我身上,如果当前 srv 版本连接字符串对您来说很好,请跳过此步骤)是即使它似乎正在添加数据我从邮递员发送到 Atlas,我总是收到以下错误:

"code": 79,
"codeName": "UnknownReplWriteConcern",
//some other lines

因此,在 MongoDB Atlas 支持团队的建议下,我只是w=majority从连接字符串中删除,这似乎解决了这个问题。

// &w=majority removed
mongodb+srv://someClusterAdmin:somePassword@someCluster.vwieg.mongodb.net/someName?retryWrites=true

推荐阅读