首页 > 解决方案 > 连接到 mongodb.com 的未处理的承诺拒绝

问题描述

我有一个本地角度项目,连接到我的 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.jsangularmongodbheroku

解决方案


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

MongoNetworkError: connection 5 to cluster closed

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


推荐阅读