node.js - 连接到 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));
解决方案
感谢@jonrsharpe,我设法添加了对err
. 然后这向我显示了连接错误:
MongoNetworkError: connection 5 to cluster closed
看起来我需要通过转到我的 mongodb 集群中的安全选项卡来将我的 heroku 实例的 ip 列入白名单。
推荐阅读
- c# - 选项模式 - 接口/抽象属性
- windows - 删除与我的 windows 分区重叠的 LVM 分区
- node.js - 节点以 nodemon 开头
- html - 如何制作一个灵活的块,使其具有与灵活的照片相同的宽度和高度,并将位于照片的顶部?
- java - 使用 JavaFX WebView 时未加载 iframe
- flutter - Flutter:在 null 脏状态下调用 getter 'length'
- python - python print() 被CPython编译成.so后的输出元组
- tensorflow - 人工智能玩连接四的神经网络:如何编码输入和输出,以及什么样的神经网络设置?
- java - 处理 onClick 和双击
- python - 如何在python中剪切一个熊猫列,并使另一列的长度相等?