【问题标题】:Unhandled promise rejection connecting to mongodb.com连接到 mongodb.com 的未处理的承诺拒绝
【发布时间】:2019-08-02 17:59:23
【问题描述】:

我有一个本地 Angular 项目,连接到我的 mongodb,但是当尝试连接到 mongodb.com 上的远程项目时,我收到未处理的承诺异常:

(node:23) UnhandledPromiseRejectionWarning: ReferenceError: err is not defined at mongoose.connect.then.catch (/app/app.js:27:26) 

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

此代码似乎有问题,但我不确定要更改什么,因为这在本地有效。在 heroku 上运行时,出现错误。

// Map global promise - get rid of warning
mongoose.Promise = global.Promise;
// Connect to mongoose
mongoose.connect("mongodb+srv://<username>:<password>@cluster0-cx2dj.mongodb.net/test?retryWrites=true", {
    useNewUrlParser: true
})
.then(() => console.log('MongoDb Connected...'))
.catch(() => console.log(err));

【问题讨论】:

  • 不同版本的Node?此外,您只会看到 is 连接错误,您是否在本地调用过该错误?
  • 啊,我明白了。如果我在本地断开与 mongodb 的连接,我会收到该错误。我不知道为什么我可以在本地连接但不能通过 Heroku。
  • 如果您确实将err 添加到.catch 回调参数中,您可能会看到更多信息。
  • 感谢您的建议,我是 Angular 新手,错过了这个错误。添加对 err 的缺失引用告诉我一个错误,我可以更好地调试。请看我的回答。

标签: node.js angular mongodb heroku


【解决方案1】:

感谢@jonrsharpe,我设法添加了对err 的引用。然后这向我显示了连接错误:

MongoNetworkError: connection 5 to cluster closed

看来我需要通过转到我的 mongodb 集群中的安全选项卡来将我的 heroku 实例的 ip 列入白名单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2023-03-17
    • 2018-04-01
    • 2021-09-19
    • 1970-01-01
    相关资源
    最近更新 更多