未处理的承诺拒绝连接到mongodb.com

时间:2019-03-12 09:50:29

标签: node.js angular mongodb heroku

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

(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));

1 个答案:

答案 0 :(得分:0)

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

MongoNetworkError: connection 5 to cluster closed

似乎我需要通过进入mongodb群集中的“安全性”选项卡将heroku实例的IP列入白名单。