【问题标题】:UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server on first connect with Heroku serverUnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server on first connect with Heroku server
【发布时间】:2020-02-06 04:07:49
【问题描述】:

我正在尝试使用通过 GitHub 部署在 Heroku 上的 NodeJS 应用程序访问我的 MongoDB 集群。无论我尝试什么,只要应用程序启动,就会抛出此错误:

UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server on first connect

我试图访问它的代码如下:

//Connect to mongo and create a connection
const MongoClient = require('mongodb').MongoClient;

const mongoClient = new MongoClient(process.env.connectionString, { useNewUrlParser: true });
const connection = mongoClient.connect();

我已经完成了控制台日志,以确保 process.env.connectionString 包含我到 Mongo 集群的连接字符串。我还确保我的集群允许所有传入连接。还有什么我遗漏的,还是我完全不在基地?

【问题讨论】:

    标签: node.js mongodb heroku heroku-cli


    【解决方案1】:

    请使用 Heroku mongolab 插件。将本地 mongodb 导出到 Heroku 生成的新 mongolab db。它应该很简单。

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2020-12-07
      • 2020-11-03
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多