mongodb - 从 Postman 向 Heroku 发送请求返回 503 和/或 UnknownReplWriteConcern
问题描述
我正在尝试从 Postman 向 Heroku 应用程序发送发布请求,但它一直返回503 status error
with 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 白名单。
什么可能导致此错误?
解决方案
因此,在 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
推荐阅读
- python - 从 django ORM 中检索具有相关字段的查询
- postgresql - 使用 apache-nifi 将两列加在一起
- amazon-web-services - 来自 CloudWatch 规则的 AWS SAM Lambda 函数触发器
- blender - 您如何使用颜色渐变连续随机更改光点的颜色?
- javascript - 如何插入 @Html.DisplayFor(m => m.EventDetailSection.ContactAccountTel)
- javascript - 我究竟做错了什么。我想显示没有时间戳的日期但添加 50 天
- maven - 如何在 maven repo 下获取所有 pom.xml
- c# - 将用户名和密码移动到 ASP.NET Core Identity
- python - chrome 驱动程序无头选项不适用于链接?
- react-native - 更改 TextInput onFocus 和 onBlur.React-Native 时的样式