代码之家  ›  专栏  ›  技术社区  ›  AndrewL64

从邮递员向Heroku应用发送post请求返回503状态错误

  •  0
  • AndrewL64  · 技术社区  · 6 年前

    我正试图从邮递员发送一个post请求到Heroku应用程序,但它一直返回一个 503 status error 具有 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白名单中。


    0 回复  |  直到 6 年前
        1
  •  1
  •   AndrewL64    5 年前

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

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

    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版本连接字符串对您运行良好,请跳过此步骤)是,即使它似乎在添加我从postman发送到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